15.8k views
1 vote
Write a program that creates a dictionary containing the U.S. states as keys and their abbreviations as values. The program should then randomly quiz the user by displaying the abbreviation and asking the user to enter that state's name. The program should keep a count of the number of correct and incorrect responses, as well as which abbreviations the user missed. Display the number of correct and incorrect responses each round. Ask the user if they want to keep playing each round. When they say they want to stop playing, display the missed abbreviations. You should include the following:

Mainline logic and functions
Error handling - this is pretty light for this lab, see the rubric below
Appropriate comments and shebang line
At least one dictionary
The code needs to be written in Python

1 Answer

5 votes

Final answer:

The requested Python program quizzes the user on U.S. state names based on abbreviations, keeping track of correct and incorrect answers, and displaying missed abbreviations after the user chooses to stop playing. Minimal error handling is included, and the program utilizes a dictionary, functions, input prompts, and loops.

Step-by-step explanation:

The task is to write a Python program that quizzes the user on the full names of U.S. states given their abbreviations. First, a dictionary containing states as keys and their abbreviations as values is created. Then, users are randomly quizzed, with counts for correct and incorrect responses. Users can choose to keep playing after each round, and missed abbreviations are shown at the end. The program includes error handling for user input and uses functions for organizing the code. Comments are used for clarity, and a shebang line indicates the script's interpreter.

Error Handling

Minimal error handling is expected, likely just ensuring the user enters valid data when prompted for input.

Example Code

#!/usr/bin/env python3import random
def load_states():
# Load the dictionary with states and abbreviations
return {
'Alabama': 'AL',
...
}

def quiz_user(states):
# Main quiz logic
correct = 0
incorrect = 0
missed = []
while True:
state_abbr, state_name = random.choice(list(states.items()))
guess = input(f"What is the full name of the state with the abbreviation {state_abbr}? ")
# Check if correct
if guess.lower() == state_name.lower():
correct += 1
else:
incorrect += 1
missed.append(state_abbr)

print(f"Correct: {correct}, Incorrect: {incorrect}")
# Ask user to continue playing
if input("Keep playing? (y/n): ").lower() != 'y':
break

print("Missed abbreviations:", missed)

def main():
states = load_states()
quiz_user(states)

if __name__ == '__main__':
main()

User GenesisST
by
7.5k points