I’m making a blackjack code and having problems with my function. Whenever I call this function, the first time it works and the value is saved but when it appears the 2nd time it adds the first total AGAIN along with the 2nd total.
eg 1st card = 7, total = 7, 2nd card = 3, total = 17 (it should be 10)
# creating deck of cards
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
rank_values = {
'A': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'J': 10,
'Q': 10,
'K': 10
}
# function to deal cards
def deal_card(person, total):
random_value = random.choice(ranks)
print(player, "card is", random_value)
# problem line below
total = total + rank_values[random_value]
return total
playertotal = 0
for i in range(2):
playertotal+=deal_card("player", playertotal)
print(playertotal)
I’ve tried formatting the function differetly and moving the problem line out of the function. Nothing new happened sadly.
New contributor
Meems is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.