[Tutor] creating a dictionary for capital quiz program

Stephanie Quiles stephanie.quiles001 at albright.edu
Tue Jun 2 01:19:41 CEST 2015


Good evening, 

As you may have noticed i am really struggling with functions and dictionaries. I need to figure out why this program is allowing me to continue entering incorrect data instead of telling me my answer is incorrect. also at the end it’s not tallying the incorrect/correct responses properly. please any help would be appreciated. 

Write a program that creates a dictionary containing the U.S. States as keys and their
capitals as values.
(Use the internet to get a list of the states and their capitals.)
The program should then randomly quiz the user by displaying the name of a state and asking
the usr to enter that state's capital.
The program should keep a count of the number of correct and incorrect responses.
(As an alternative to the US states, the program can use the names of countries and
their capitals.)"""

import pickle


def main():

    right = 0
    wrong = 0
    capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \
 \
               "Arizona": 'Phoenix', \
 \
               'Arkansas': 'Little Rock', 'California': 'Sacramento', \
 \
               'Colorado': 'Denver', \
 \
               'Connecticut': 'Hartford', 'Delaware': 'Dover', \
 \
               'Florida': 'Tallahassee', \
 \
               'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', \
 \
               'Idaho': 'Boise', \
 \
               'Illinois': 'Springfield', 'Indiana': 'Indianapolis', \
 \
               'Iowa': 'Des Moines', \
 \
               'Kansas': 'Topeka', 'Kentucky': 'Frankfort', \
 \
               'Louisiana': 'Baton Rouge', \
 \
               'Maine': 'Augusta', 'Maryland': 'Annapolis', \
 \
               'Massachusetts': 'Boston', \
 \
               'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', \
 \
               'Mississippi': 'Jackson', \
 \
               'Missouri': 'Jefferson City', 'Montana': 'Helena', \
 \
               'Nebraska': 'Lincoln', \
 \
               'Nevada': 'Carson City', 'New Hampshire': 'Concord', \
 \
               'New Jersey': 'Trenton', \
 \
               'New Mexico': 'Santa Fe', 'New York': 'Albany', \
 \
               'North Carolina': 'Raleigh', \
 \
               'North Dakota': 'Bismarck', 'Ohio': 'Columbus', \
 \
               'Oklahoma': 'Oklahoma City', \
 \
               'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', \
 \
               'Rhode Island': 'Providence', \
 \
               'South Carolina': 'Columbia', \
 \
               'South Dakota': 'Pierre', 'Tennessee': 'Nashville', \
 \
               'Texas': 'Austin', 'Utah': 'Salt Lake City', \
 \
               'Vermont': 'Montpelier', \
 \
               'Virginia': 'Richmond', 'Washington': 'Olympia', \
 \
               'West Virginia': 'Charleston', \
 \
               'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}

    for k in capitals.keys():
        state = input('Enter the capital of '+k+' :')
    if state.upper() == capitals[k].upper():
        right += 1
        print('Correct')
    else:
        wrong += 1
        print('Incorrect')
    choice = input('Do you want to play again y/n: ')
    if choice.upper() == 'N':
        print('end of game')
    else:
        choice.upper() != 'Y'
        print("invalid choice")

    print('Number of correct answers is: ', right)
    print("Number of incorrect answers is:", wrong)

main()





More information about the Tutor mailing list