python - What is wrong with my dictionary (mapping values to cards) -


so game blackjack , have snippets of code make deck , make hand. deck, list, i'm trying build dictionary each card (a tuple deck) have value mapped it, per rules of blackjack.

from random import randint  def make_deck():     deck = []     suit in suits:         rank in ranks:             deck.append((suit,rank))     return deck  suits = ['spades','hearts','diamonds','clubs'] ranks = ['ace','two','three','four','five','six','seven','eight','nine','ten','jack','queen','king']  deck = make_deck()  def make_hand():     hand = []     k in range(2):         card = deck.pop(randint(0,51))         hand.append(card)     return hand  hand = make_hand()  values = {} #empty dictionary card in deck:     rank = [card[1] card in deck]     if rank == 'ace':         values[card] = 1     elif rank == 'two':         values[card] = 2     elif rank == 'three':         values[card] = 3     elif rank == 'four':         values[card] = 4     elif rank == 'five':         values[card] = 5     elif rank == 'six':         values[card] = 6     elif rank == 'seven':         values[card] = 7     elif rank == 'eight':         values[card] = 8     elif rank == 'nine':         values[card] = 9     elif rank == 'ten' or 'jack' or 'queen' or 'king':         values[card] = 10  print values 

obviously, creating dictionary brute force , lacks elegance pointers more efficient manner appreciated. real issue can't identify wrong code print values returns 1 tuple mapped value rather entire dictionary. if move list comprehension rank outside of loop, dictionary mapped value 10.

also, realized make_hand() function results in error "the pop index out of range," advice on things try work time appreciated.

replace line:

rank = [card[1] card in deck] 

with:

rank = card[1] 

and yes, can more efficiently (and elegantly) creating dictionary maps values scores:

mapper = {'ace': 1, 'two': 2, ...} 

and don't need if/elif...else:

for card in deck:     values[card] = mapper[card[1]] 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -