Capital Quiz python Write a program that creates a dictionary containing the U.S
ID: 3918086 • Letter: C
Question
Capital Quiz python
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 user 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 U.S. states, the program can use the names of countries and their capitals.)
Explanation / Answer
from random import randint def main(): state_capitals = {"Washington": "Olympia", "Oregon": "Salem", "California": "Sacramento", "Ohio": "Columbus", "Nebraska": "Lincoln", "Colorado": "Denver", "Michigan": "Lansing", "Massachusetts": "Boston", "Florida": "Tallahassee", "Texas": "Austin", "Oklahoma": "Oklahoma City", "Hawaii": "Honolulu", "Alaska": "Juneau", "Utah": "Salt Lake City", "New Mexico": "Santa Fe", "North Dakota": "Bismarck", "South Dakota": "Pierre", "West Virginia": "Charleston", "Virginia": "Richmond", "New Jersey": "Trenton", "Minnesota": "Saint Paul", "Illinois": "Springfield", "Indiana": "Indianapolis", "Kentucky": "Frankfort", "Tennessee": "Nashville", "Georgia": "Atlanta", "Alabama": "Montgomery", "Mississippi": "Jackson", "North Carolina": "Raleigh", "South Carolina": "Columbia", "Maine": "Augusta", "Vermont": "Montpelier", "New Hampshire": "Concord", "Connecticut": "Hartford", "Rhode Island": "Providence", "Wyoming": "Cheyenne", "Montana": "Helena", "Kansas": "Topeka", "Iowa": "Des Moines", "Pennsylvania": "Harrisburg", "Maryland": "Annapolis", "Missouri": "Jefferson City", "Arizona": "Phoenix", "Nevada": "Carson City", "New York": "Albany", "Wisconsin": "Madison", "Delaware": "Dover", "Idaho": "Boise", "Arkansas": "Little Rock", "Louisiana": "Baton Rouge"} states = list(state_capitals.keys()) correct = 0 total = 0 while True: state = states[randint(0, len(states) - 1)] cap = input('What is the capital of ' + state + ': ') if cap.lower() == state_capitals[state].lower(): correct += 1 print("Yes, That's correct") else: print("No it's " + state_capitals[state]) total += 1 choice = input('Do you want to try again(y or n): ') if choice.lower() == 'n': break print() print('You got %d out of %d correct!' % (correct, total)) main()