python - Return is always giving me the same value -
my return value printing out total of 20 regardless of cards in user_hand. reason why?
suits = ["heart", "diamond", "spade", "club"] ranks = ['a', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k'] deck = [(suit, rank) rank in ranks suit in suits] random.shuffle(deck,random.random) user_hand = [] dealer_hand = [] user_hand.append(deck.pop()) dealer_hand.append(deck.pop()) user_hand.append(deck.pop()) dealer_hand.append(deck.pop()) def handtotal (hand): total = 0 rank in hand: if rank == "j" or "q" or "k": total += 10 elif rank == 'a' , total < 11: total += 11 elif rank == 'a' , total >= 11: total += 1 elif rank == '2': total += 2 elif rank == '3': total += 3 elif rank == '4': total += 4 elif rank == '5': total += 5 elif rank == '6': total += 6 elif rank == '7': total += 7 elif rank == '8': total += 8 elif rank == '9': total += 9 return total print ("your current hand {}".format(user_hand)) print ("this provides total of:") print (handtotal(user_hand))
one of problems line:
if rank == "j" or "q" or "k"
as stands, always evaluate true (well, technically "q"). should comparing rank
of variables:
if rank == "j" or rank == "q" or rank == "k"
or more pythonic way:
if rank in ["j", "q", "k"]
in addition, you're passing user_hand
handtotal()
. since each element of user_hand
tuple, you'll need compare against second element of each element (e.g. rank[1]
) of user_hand
.
Comments
Post a Comment