Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

default value from get() method

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The int conversion is done before ever calling get, you can just do it after retrieving the value

hand += int(values.get(card, card))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading