Answer:
Check the attached image below to see the screenshot
Step-by-step explanation:
# Create a dictionary of US states as key and capitals as values
- capitals_dict = {"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": "St. 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"}
# Number of questions to be asked in quiz
# You can change it accordingly
numberOfQuestion = 10
# Number of correct responses by user
correctResponse = 0
# Number of correct responses by user
incorrectResponse = 0
# Counter to keep track of number of questions asked
i = 0
# List of the states already asked
list_capitals_asked = []
while i < numberOfQuestion:
i += 1 # Increment i by 1
# Select a state randomly from dictionary
state = random.choice(list(capitals_dict.keys()))
# Check if list already contains this state
# If yes, select another state
# Keep doing that, until a new state is found
while list_capitals_asked.__contains__(state):
state = random.choice(list(capitals_dict.keys()))
# Add state to list
list_capitals_asked.append(state)
# Get capital of state
correct_ans = capitals_dict[state]
# Ask user to enter capital of state
user_ans = input('What is the capital of ' + state + '? ')
# If user answer's matches correct answer
# Increment correctResponse by 1
# Else, Increment incorrectResponse by 1
if user_ans == correct_ans:
correctResponse += 1
else:
incorrectResponse += 1
# Print correct and incorrect responses
print('Correct Responses:', correctResponse)
print('Incorrect Responses:', incorrectResponse)