Question

For this lab, write a program that lets the user enter a state and the program...

For this lab, write a program that lets the user enter a state and the program returns that state's capital, stored in a dictionary

Requirements:

  • Start by hard coding in all of the states and their capitals
  • the user should be able to enter the name of a state or ask for all states to be printed (one key/value pair per line)
  • If the state is in the dictionary, print its state capital in the form: "The capital of Vermont is Montpelier"
  • If the state is not in the list, print out a message saying it is not in the list
  • Let the program repeat so the user can look up multiple states

Optional Extra Challenge:

  • Also print out the state's abbreviation: Washington is WA, New York is NY, etc.
  • Allow the user to add/remove states
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Explanation:

state_capital is the hardcoded dictionary. while loop is used to ask the user again and again to try.

2 options are given to the user. To search for capital or to print all state and capitals.


state_capital={
'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',
'Illinios': 'Springfield',
'Indiana': 'Indianapolis',
'Iowa': 'Des Monies',
'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',
'Neveda': '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',
'Rhoda Island': 'Providence',
'South Carolina': 'Columbia',
'South Dakoda': 'Pierre',
'Tennessee': 'Nashville',
'Texas': 'Austin',
'Utah': 'Salt Lake City',
'Vermont': 'Montpelier',
'Virginia': 'Richmond',
'Washington': 'Olympia',
'West Virginia': 'Charleston',
'Wisconsin': 'Madison',
'Wyoming': 'Cheyenne'
}


option = 'y'

while(option=='y'):
  
print('1. Search for a state')
print('2. Print all state and capitals')
n = input('Choose an option(1/2): ')
  
if(n=='1'):
name = input('Enter state name: ')
if name in state_capital:
print('The capital of', name, 'is', state_capital[name])
else:
print(name, 'is not in the list.')
elif(n=='2'):
for k, v in state_capital.items():
print(k, ":", v)
  
option = input('Would you like to try again?(y/n) ')


Output:

1. Search for a state
2. Print all state and capitals
Choose an option(1/2): 1
Enter state name: Australia
Australia is not in the list.
Would you like to try again?(y/n) y
1. Search for a state
2. Print all state and capitals
Choose an option(1/2): 1
Enter state name: California
The capital of California is Sacramento
Would you like to try again?(y/n) y
1. Search for a state
2. Print all state and capitals
Choose an option(1/2): 2
Montana : Helena
Michigan : Lansing
Georgia : Atlanta
New Mexico : Santa Fe
North Carolina : Raleigh
South Dakoda : Pierre
Wisconsin : Madison
Virginia : Richmond
Wyoming : Cheyenne
Hawaii : Honolulu
Vermont : Montpelier
Alabama : Montgomery
South Carolina : Columbia
California : Sacramento
Nebraska : Lincoln
Delaware : Dover
Mississippi : Jackson
Illinios : Springfield
Kansas : Topeka
Minnesota : St. Paul
Oklahoma : Oklahoma City
Washington : Olympia
Massachusetts : Boston
Utah : Salt Lake City
Neveda : Carson City
Maine : Augusta
Missouri : Jefferson City
New Jersey : Trenton
Arizona : Phoenix
Louisiana : Baton Rouge
New Hampshire : Concord
Idaho : Boise
Rhoda Island : Providence
Pennsylvania : Harrisburg
Iowa : Des Monies
West Virginia : Charleston
Oregon : Salem
Connecticut : Hartford
Arkansas : Little Rock
Florida : Tallahassee
Alaska : Juneau
North Dakota : Bismarck
Tennessee : Nashville
Maryland : Annapolis
New York : Albany
Texas : Austin
Kentucky : Frankfort
Colorado : Denver
Ohio : Columbus
Indiana : Indianapolis
Would you like to try again?(y/n) n



PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
For this lab, write a program that lets the user enter a state and the program...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT