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
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.
Get Answers For Free
Most questions answered within 1 hours.