73,154 views
0 votes
0 votes
Write a program that creates a map 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 ask 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.) -Python

User Arnaud Potier
by
2.8k points

1 Answer

2 votes
2 votes

Answer:

Here is the Python program:

import random # to generate random numbers

def quiz(): #method quiz to ask state's capital

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"}

#dictionary of states along with their capitals

quit=False #exit option

print ("Welcome to the States capitals quiz game!")

wrong_ans = False # boolean variable for incorrect answer

while not quit and len(capitals)>0:

#loop continues until all state capitals are finished and user presses q

choice = random.choice(list(capitals.keys()))

#choice variable stores randomly generated list of pick

right_ans = capitals.get(choice) #stores correct answer

print ("\\What is the capital city of",choice,"?")

user_ans = input("Your guess: ") #answer entered by user

if user_ans.lower()=='q': #if user presses q to quit

quit=True #quit value become true

print("The quiz is over! Bubye!")

#displays the above message and quits the program

elif user_ans.lower()==right_ans.lower(): #if user enters correct guess

print ("Right Answer! \\") # displays this message

else: #if user enters wrong guess

print ("Wrong Answer!\\") #displays wrong answer message

print ("The right answer is ",right_ans) #prints right capital

wrong_ans = True

quiz() #calls quiz function to begin with the quiz

Step-by-step explanation:

The program has a dictionary that contains the US states as keys and their capitals as values. The program asks the user to enter the capital of a random state. This state is selected randomly from the list of states using random function and keys() method is used to display list of all keys in the capital states dictionary. The while loop starts which keeps asking the user to guess the capital of states. This loop ends when the user types q to quit the program or all the capitals of the states are covered. Now the user guess is compared to the right answer, if the user has correctly guessed the capital then the Right answer message is displayed otherwise Wrong answer is displayed. After Wrong answer message the correct answer is displayed and the quiz starts again until the user enters q to exit. When the user types q the message: The quiz is over! Bubye! is displayed and the programs exits.

Write a program that creates a map containing the U.S. states as keys, and their capitals-example-1
User Emre Bener
by
3.2k points