Unexpected error where get() tries to evaluate the default value, despite the key existing in the dictionary
def blackjack(cards):
values = {"J" : 10, "Q" : 10, "K" : 10}
aces = cards.count("A")
cards = [i for i in cards if i != "A"]
hand = 0
for card in cards:
hand += values.get(card, int(card))
while aces > 0:
if hand + aces > 11:
return hand + aces
elif hand < 11: hand += 11
else: hand += 1
aces -= 1
return hand
cards = ['J','3']
blackjack(cards)
hand += values.get(card, int(card))
^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'J'
>Solution :
The int
conversion is done before ever calling get, you can just do it after retrieving the value
hand += int(values.get(card, card))