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
  • Create a program that solves programming exercise below. Within this folder is the file state_capitals.txt. The...

    Create a program that solves programming exercise below. Within this folder is the file state_capitals.txt. The file is organized with a state name on a line and the next line has its capital. Your program should read this in and create a dictionary from this file. Then use this dictionary to create a quiz, ask the user for 5 random choice states and give the results back at end. Write a program that creates a dictionary containing the U.S. States...

  • Python 3 **11.40 (Guess the capitals) Write a program that repeatedly prompts the user to enter...

    Python 3 **11.40 (Guess the capitals) Write a program that repeatedly prompts the user to enter a capital for a state. Upon receiving the user input, the program reports whether the answer is correct. Assume that 50 states and their capitals are stored in a two- dimensional list, as shown in Figure 11.13. The program prompts the user to answer all the states’ capitals and displays the total correct count. The user’s answer is not case sensitive. Implement the program...

  • Write a C# windows forms program to create a States and Capitals guessing game. A method...

    Write a C# windows forms program to create a States and Capitals guessing game. A method in your program should generate a random number between 1 and 50. Your program should use this number to represent one of the 50 U.S states and then display the name of that state. The user will guess the capital of that state. Another method in your program should get the users answer and check to see whether it is correct. Yet another method...

  • Create a program using Python, following the steps below: 1) Collect the Users name, and store...

    Create a program using Python, following the steps below: 1) Collect the Users name, and store that name. 2) Welcome the user, and ask if that user would like to take the Capitals quiz. 3) Create a dictionary of US states as keys and the state capitals that correspond with your list, as the values (Choose 12 of your favorite states). 4) Then have the program quiz the user, randomly asking for the capital of each of the states in...

  • In C++ Write a program that lets the user enter at least 10 values into an...

    In C++ Write a program that lets the user enter at least 10 values into an array. The program then display the largest and smallest values stored in the array.

  • Write a program that lets the user enter at least 10 values into an array. The...

    Write a program that lets the user enter at least 10 values into an array. The program should then display the largest and smallest values stored in the array. Do it in C++

  • Write a program that lets the user enter at least 10 values into an array. The...

    Write a program that lets the user enter at least 10 values into an array. The program should then display the largest and smallest values stored in the array. Do it in C++

  • Python: Write a program that lets the user enter a string and displays the letter that...

    Python: Write a program that lets the user enter a string and displays the letter that appears most frequently in the string, ignore spaces, punctuation, and Upper vs Lower case. Create a list to hold letters based on the length of the user input Convert all letters to the same case Use a loop to check for each letter in the alphabet Have a variable to hold the largest number of times a letter appears, and replace the value when...

  • Write a program that lets the user enter 10 values into an array. The program should...

    Write a program that lets the user enter 10 values into an array. The program should then display the largest and the smallest values stored in the array. The program should display the following message to the user at the beginning "This program will ask you to enter ten values. Then it will determine the largest and smallest of the values you entered.Please enter 10 integers separated by spaces: " And then after user entered the numbers, it should display...

  • Write a "PYTHON" program to prompt the user to enter a fist name, last name, student...

    Write a "PYTHON" program to prompt the user to enter a fist name, last name, student ID and GPA. Create a dictionary with the data. Print out the data. Then remove the GPA and print again.

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