python - Returning variable from function and printing said variable -
i know newbie question bare me. in code below run blackjack game , want total hand (user or dealer's) function. when run code no errors appear, when call function, total hand value not printed. states "this provides total of:" , number blank. appreciated! see code below:
user_name = input("please enter name:") print ("welcome table {}. let's deal!".format(user_name)) import random 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 (total) print ("your current hand {}".format(user_hand)) print ("this provides total of:") handtotal(user_hand)
there's not point in putting print(total)
after return total
, because return
causes function terminate , not evaluate lines following return statement*. instead, try putting print
outside of function definition:
print ("this provides total of:") print(handtotal(user_hand))
*(there corner cases try-except-finally blocks, true majority of time.)
Comments
Post a Comment