Pull randomized key, value pair from dictionary for Quiz

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()

The docs random — Generate pseudo-random numbers — Python 3.11.2 documentation tell you that choice takes a seq as an arg.

A dictionary is not a seq.

You could create a list from the keys in a dictionary and use that list.

all_keys = list(states_dict)

Note you need to call list() to turn the key iterator into a list for choice to use. Also note you do not need to call the keys() function.

You could, in fact, create a list from the items in the dictionary and then shuffle it.

Yet another way would be to index the dictionary, then pick the index number at random:

states_dict = {
    1: ("Alabama", "Montgomery"),
    2: ("Alaska", "Juneau"),
    3: ("Arizona", "Phoenix"),
    4: ("Arkansas", "Little Rock"),
    5: ("California", "Sacramento"),
    6: ("Colorado", "Denver"),
    7: ("Connecticut", "Hartford"),
    # etc
}

To add: you could simplify your if: branch:

if capital == value:
    count += 1
    print("Correct!")
    print()
else:
    num_wrong += 1
    print("Sorry, that is incorrect.")
    print()