Lauren Kim
Lauren Kim

Reputation: 9

Difficulties coding variables for the choices in a dictionary guessing game

I'm trying to create a guessing game where the user chooses a key within the dictionary and guesses the value of it. I'm having a hard time trying to code the variables for the choice in order to have to guess the value.

Here is what I have so far:

def main():
    capitals = {'AL' : 'Montgomery', 'AK' : 'Juneau', 'AZ' : 'Phoenix',
                'AR' : 'Little Rock', 'CA' : 'Sacramento', 'CO' : 'Denver',
                'CT' : 'Hartford', 'FL' : 'Tallahassee', 'GA' : 'Atlanta',
                'HI' : 'Honolulu', 'ID' : 'Boise', 'IL' : 'Springfield',
                'IN' : 'Indianapolis', 'IA' : 'Des Moines', 'KS' : 'Topeka'}
    print("Choose a state from the list", capitals.keys())
    choice = input('Which state would you like to guess? : ')
    choice == capitals.keys()
    guess = input('Guess the capital of the state chosen : ')
    answer = capitals.values()
    if guess == answer:
        print("Yay! You're CORRECT!")
    else:
        print("Sorry, You're INCORRECT!")

main()

It seems that my if statement isn't being read by the program. How can I fix this?

Upvotes: 0

Views: 39

Answers (2)

Apollo2020
Apollo2020

Reputation: 519

There are a few things wrong with the code provided. Here's a working version with inline comments to explain what's going on. While loops allow you to check the input data before moving on.

def main():

    capitals = {'AL': 'Montgomery', 'AK': 'Juneau', 'AZ': 'Phoenix',
                'AR': 'Little Rock', 'CA': 'Sacramento', 'CO': 'Denver',
                'CT': 'Hartford', 'FL': 'Tallahassee', 'GA': 'Atlanta',
                'HI': 'Honolulu', 'ID': 'Boise', 'IL': 'Springfield',
                'IN': 'Indianapolis', 'IA': 'Des Moines', 'KS': 'Topeka'}

    # initialize variables used in while loops
    choice = '' 
    guess = ''

    print("Choose a state from the list: {0}".format(' '.join(capitals.keys())))

    # as long as choice is not in the list of capitals, keep asking
    while choice not in capitals.keys():
        choice = input('Which state would you like to guess? : ')

    # get the correct answer for the chosen state
    answer = capitals[choice]

    # as long as the guess doesn't contain any non-whitespace characters, keep asking
    while guess.strip() == '':
        guess = input('Guess the capital of the state chosen : ')

    # if the cleaned up, case-insensitive guess matches the answer, you win
    if guess.strip().lower() == answer.lower():
        print("Yay! You're CORRECT!")

    # otherwise, you lose
    else:
        print("Sorry, You're INCORRECT!")

main()

Upvotes: 0

Prune
Prune

Reputation: 77885

Your semantics are incorrect. Just before your if statement, insert

print (guess, answer)

This is basic debugging. You'll see the problem: answer is a list of all the capitals; there's no way that raw user input can be equal to an entire list. You need to compare against only the capital for that one state.

You'll have to make a similar check on choice, as you've made teh same mistake there.

Upvotes: 1

Related Questions