Question

Write a Python  program with correct indentation that creates a dictionary containing the U.S. states as keys...

  1. Write a Python  program with correct indentation 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
  2. A file is attached which contains the states and abbreviations to use as a reference if needed. ( No actual file included so the information containing the file is listed below
    Alabama: AL
    Alaska: AK
    Arizona: AZ
    Arkansas: AR
    California: CA
    Colorado: CO
    Connecticut: CT
    Delaware: DE
    Florida: FL
    Georgia: GA
    Hawaii: HI
    Idaho: ID
    Illinois: IL
    Indiana: IN
    Iowa: IA
    Kansas: KS
    Kentucky: KY
    Louisiana: LA
    Maine: ME
    Maryland: MD
    Massachusetts: MA
    Michigan: MI
    Minnesota: MN
    Mississippi: MS
    Missouri: MO
    Montana: MT
    Nebraska: NE
    Nevada: NV
    New Hampshire: NH
    New Jersey: NJ
    New Mexico: NM
    New York: NY
    North Carolina: NC
    North Dakota: ND
    Ohio: OH
    Oklahoma: OK
    Oregon: OR
    Pennsylvania: PA
    Rhode Island: RI
    South Carolina: SC
    South Dakota: SD
    Tennessee: TN
    Texas: TX
    Utah: UT
    Vermont: VT
    Virginia: VA
    Washington: WA
    West Virginia: WV
    Wisconsin: WI
    Wyoming: WY

Homework Answers

Answer #1

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

state_abb=list(d.values())#get the abbrevations to list
state=list(d.keys())#get the names of states to list
c=0
i=0
cnt=0
l=[]
while cnt!=10:#looop ietarets 10 times
cnt+=1
r=random.randint(0,len(state_abb)-1)#to generate a random abbrevation
print("The abbreviation is:",state_abb[r])
print("Enter the state name:")
s=input()#read input
try:
if isinstance(s,int):#check if input is valid and not an integer
raise ValueError
except ValueError:
print("Enter valid input")
  
if s.lower()==state[state_abb.index(state_abb[r])].lower():#check name and abbrevation are correct
c+=1
else:
i+=1
l.append((state[state_abb.index(state_abb[r])],state_abb[r]))#append name and abbrevation to list if incorrect input is made by user

print("The number of correct guesses:",c)
print("The number of incorrect guesses:",i)#print the details on console
print("The following are the missed guesses")
for i in l:
print(i[0],":",i[1])
  

Screenshots:

The screenshots are attached below for reference.

Please follow them for output and proper indentation.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
database design you are tasked with creating a logical database design that will be used later...
database design you are tasked with creating a logical database design that will be used later to implement the physical database named State_Capitals. The first step of creating a logical database design is normalization. States of the United States Country Region States United States Midwest Iowa (IA), Illinois (IL), Indiana (IN), Kansas (KS), Michigan (MI), Minnesota (MN), Missouri (MO), North Dakota (ND), Nebraska (NE), Ohio (OH), South Dakota (SD), Wisconsin (WI) United States Northeast Connecticut (CT), Delaware (DE), Massachusetts (MA),...
The ages for each of the fifty states in the US are shown in Table 2...
The ages for each of the fifty states in the US are shown in Table 2 Construct a frequency and a percent frequency distribution of the ages begin the first class at 20.0 and use a class width of 9. Construct a histogram. Comment on the shape of the distribution. which state has the highest age? which state has the lowest age? Table 2 Alabama 76.2 Montana 73.6 Alaska 65.9 Nebraska 78.3 Arizona 79.7 Nevada 75.1 Arkansas 70.9 New Hampshire...
Data showing the population by state in millions of people follow (The World Almanac, 2012). The...
Data showing the population by state in millions of people follow (The World Almanac, 2012). The dataset in Excel file 2012Population.xlsx. a. Develop a frequency distribution, a percent frequency distribution, and a histogram. Use a class width of 2.5 million. b. Does there appear to be any skewness in the distribution? Explain.   c. What observations can you make about the population of the 50 states? State Population Alabama 4.8 Alaska 0.7 Arizona 6.4 Arkansas 2.9 California 37.3 Colorado 5 Connecticut...
The homeownership rate in the U.S. was 64.4% in 2009. In order to determine if homeownership...
The homeownership rate in the U.S. was 64.4% in 2009. In order to determine if homeownership is linked with income, 2009 state-level data on the homeownership rate (Ownership in %) and median household income (Income in $) were collected. A portion of the data is shown in the accompanying table. State Ownership Income Alabama 70.4 37990 Alaska 64.6 59614 Arizona 65.9 43749 Arkansas 64.8 34548 California 55.0 54144 Colorado 66.0 53940 Connecticut 68.3 62861 Delaware 73.6 50124 District of Columbia...
The homeownership rate in the U.S. was 60.6% in 2009. In order to determine if homeownership...
The homeownership rate in the U.S. was 60.6% in 2009. In order to determine if homeownership is linked with income, 2009 state-level data on the homeownership rate (Ownership in %) and median household income (Income in $) were collected. A portion of the data is shown in the accompanying table. State Ownership Income Alabama 65.8 35,500 Alaska 61.9 57,124 ⋮ ⋮ ⋮ Wyoming 67.5 47,990 a-1. Estimate the model Ownership = β0 + β1Income + ε. (Negative values should be...
Census data was collected on the 50 states and Washington, D.C. We are interested in determining...
Census data was collected on the 50 states and Washington, D.C. We are interested in determining whether average lifespan (LIFE) is related to the ratio of males to females in percent (MALE), birth rate per 1,000 people (BIRTH), divorce rate per 1,000 people (DIVO), number of hospital beds per 100,000 people (BEDS), percentage of population 25 years or older having completed 16 years of school (EDUC) and per capita income (INCO), with LIFE (y) as the response variable, and MALE...