Home > OS >  Why is the key not being added to the dictionary? Is it a scope issue?
Why is the key not being added to the dictionary? Is it a scope issue?

Time:08-08

I am trying to create a Blackjack game, but in Blackjack, the Ace can be either 1 point or 11. I tried to find a way to incorporate the flexibility and I thought it would work since I was adding the key into the dictionary. A KeyError is popping up however when the code is run. I am not sure how to fix this issue.

# imports random module
import random

# array of the different possible cards
cards = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]

# determine card values
card_values = {"2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10, "Jack": 10, "Queen": 10, "King": 10}

# FIGURE OUT ACE PROBLEM

# gets first two player cards
card_1 = cards[random.randint(0, 12)]
card_2 = cards[random.randint(0, 12)]

# tells player what their cards are
print(card_1)
print(card_2)

# deals with Ace
if card_1 == "Ace" or card_2 == "Ace":
    user_choice = input("Would you like the value to be 1 or 11?: ")
    if user_choice == 1:
        card_values["Ace"] = 1
    if user_choice == 11:
        card_values["Ace"] = 11

# sum of the values
sum = card_values[card_1]   card_values[card_2]

# outcome if sum is less than 21
while sum < 21:
    user_choice = input("Would you like to hit or pass?: ")
    if user_choice == "hit":
        card = cards[random.randint(0, 12)]
        print(card)
        if card == "Ace":
            user_choice = input("Would you like the value to be 1 or 11?: ")
            if user_choice == 1:
                sum  = 1
            if user_choice == 11:
                sum  = 11
        sum  = card_values[card]
    if user_choice == "pass":
        print("You ended your turn.")

# if sum is 21
if sum == 21:
    print("Congratulations! You won!")
    
# if sum goes over 21
if sum > 21:
    print("You busted! Try again.")

CodePudding user response:

a dictionary is a hash map you cant add a key without a value just add the "Ace" with andy place holder like this and you should be fine


# imports random module
import random

# array of the different possible cards
cards = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]

# determine card values
card_values = {"Ace":0, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10, "Jack": 10, "Queen": 10, "King": 10}

# FIGURE OUT ACE PROBLEM

# gets first two player cards
card_1 = cards[random.randint(0, 12)]
card_2 = cards[random.randint(0, 12)]

# tells player what their cards are
print(card_1)
print(card_2)

# deals with Ace
if card_1 == "Ace" or card_2 == "Ace":
    user_choice = input("Would you like the value to be 1 or 11?: ")
    if user_choice == 1:
        card_values["Ace"] = 1
    if user_choice == 11:
        card_values["Ace"] = 11

# sum of the values
sum = card_values[card_1]   card_values[card_2]

# outcome if sum is less than 21
while sum < 21:
    user_choice = input("Would you like to hit or pass?: ")
    if user_choice == "hit":
        card = cards[random.randint(0, 12)]
        print(card)
        if card == "Ace":
            user_choice = input("Would you like the value to be 1 or 11?: ")
            if user_choice == 1:
                sum  = 1
            if user_choice == 11:
                sum  = 11
        sum  = card_values[card]
    if user_choice == "pass":
        print("You ended your turn.")

# if sum is 21
if sum == 21:
    print("Congratulations! You won!")
    
# if sum goes over 21
if sum > 21:
    print("You busted! Try again.")

CodePudding user response:

The fact that you have two possible values for one choice, the Ace in this case, implies that a dictionary is not the best option for this use-case. Furthermore, in your code you're not actually using the dictionary to handle the Ace-case.

As to the specific error you're having, my guess is that this is the result of "Ace" not being in the dictionary. This could happen if the Ace is not one of the first two cards the player gets but comes up on a later hit. Simply change your if-statement to this:

if card == "Ace":
    user_choice = input("Would you like the value to be 1 or 11?: ")
    if user_choice == 1:
        sum  = 1
    if user_choice == 11:
        sum  = 11
else:
    sum  = card_values[card]

All I did here was insert an else statement so your sum isn't updated twice when an Ace comes up. This also means you don't need to store the Ace's value in the dictionary.

As an aside, you're only using random.randint to get an index for your card so you should use random.choice instead.

  • Related