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 abbreviation the user missed. You should include the following: Mainline logic and functions Error handling Dictionaries

Respuesta :

Answer:

Explanation:

The following program is written in Python. It creates a loop that keeps creating a random number to randomly choose a State abbreviation from the dictionary. Then it asks the user what the state is. If the user gets the answer correct it prints out "Correct" and adds 1 point to the correct variables. Otherwise it prints "Incorrect" and adds 1 point to the incorrect variable. Finally, printing out the final amount of correct and incorrect responses. The program was tested and the output can be seen below.

import random

states = {"AL":"Alabama","AK":"Alaska","AZ":"Arizona","AR":"Arkansas","CA":"California","CO":"Colorado","CT":"Connecticut","DE":"Delaware","FL":"Florida","GA":"Georgia","HI":"Hawaii","ID":"Idaho","IL":"Illinois","IN":"Indiana","IA":"Iowa","KS":"Kansas","KY":"Kentucky","LA":"Louisiana","ME":"Maine","MD":"Maryland","MA":"Massachusetts","MI":"Michigan","MN":"Minnesota","MS":"Mississippi","MO":"Missouri","MT":"Montana","NE":"Nebraska","NV":"Nevada","NH":"New Hampshire","NJ":"New Jersey","NM":"New Mexico","NY":"New York","NC":"North Carolina","ND":"North Dakota","OH":"Ohio","OK":"Oklahoma","OR":"Oregon","PA":"Pennsylvania","RI":"Rhode Island","SC":"South Carolina","SD":"South Dakota","TN":"Tennessee","TX":"Texas","UT":"Utah","VT":"Vermont","VA":"Virginia","WA":"Washington","WV":"West Virginia","WI":"Wisconsin","WY":"Wyoming"}

keys_list = list(states)

correct = 0

incorrect = 0

while True:

   randNum = random.randint(0, 49)

   abbr = keys_list[randNum]

   answer = input("Enter The State name of " + abbr + ": ")

   if answer.lower() == states[abbr].lower():

       print("Correct")

       correct += 1

   else:

       print("Incorrect")

       incorrect += 1

   again = input("Play again? y/n ")

   if again.lower() == 'n':

       break

print("Correct: " + str(correct))

print("Incorrect: " + str(incorrect))

ACCESS MORE