I am fairly new to python and just started using dictionaries. I need to randomize the key, value pair from the dictionary, but still be able to get the correct value for the randomized key. It is a states and capitals quiz. When I try random.choice() function, it gives a KeyValue Error.
import random
#Create states and capitals dictionary
states_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"}
def main():
count = 0
num_wrong = 0
for i in range(10):
key, value = states_dict.popitem()
print("Enter the capital for: ",key)
capital = input("Enter the capital: ")
print()
if capital == value:
count += 1
print("Correct!")
print()
if capital != value:
num_wrong += 1
print("Sorry, that is incorrect.")
print()
score = count * 10
print(f"Number correct: {count}")
print(f"Number wrong: {num_wrong}")
print(f"Your score is: {score}%")
if __name__=="__main__":
main()