In: Computer Science
Python
- Create/Initialize a dictionary with 5 state/capital pairs of your choosing. The state is the key and the capital is the value (e.g. “Kansas”:”Topeka”). Only one statement is needed to do this.
- Then prompt the user for a state.
e.g. The output
Enter a state, and I will display the capital.
State: Colorado
Colorado's capital is Denver.
- If that state exists in the dictionary, display the capital, else display "I'm sorry, but I still need to record " followed by the state entered.
e.g. the output
Enter a state, and I will display the capital.
State: Nebraska
I'm sorry, but I still need to record Nebraska.
- Modify the else to also include the ability to add the state and capital to the dictionary.
e.g. the output
Enter a state, and I will display the capital.
State: Nebraska
I'm sorry, but I still need to record Nebraska.
What is Nebraska's capital? Lincoln
The recorded states and capitals thus far are:
{'Kansas': 'Topeka', 'Iowa': 'Des Moines', 'Michigan': 'Lansing', 'California': 'Sacramento', 'Colorado': 'Denver', 'Nebraska': 'Lincoln'}
Hey, i tried to solve the problem as you have discussed.
I hope my answer will help you.
If you need any more help with the solution comment here and i will be happy to help you
thanks.
Here is the solution
stateCapitals = {
"utah": "Salt Lake City",
"Arizona": "Pheonix",
"California": "Sacramento",
"Georgia" :"Atlanta",
"indiana":"Indianapolis"
}
print('Enter a state, and I will display the capital')
state = input()
x = stateCapitals.get(state)
if x==None:
print("I'm sorry, but I still need to record",state)
print('What is',state,"'s capital")
capital = input()
stateCapitals[state]=capital
print("The recorded states and capitals thus far are:\n",stateCapitals)
else :
print(state,"'s capital is",x)
stateCapitals = {
"utah": "Salt Lake City",
"Arizona": "Pheonix",
"California": "Sacramento",
"Georgia" :"Atlanta",
"indiana":"Indianapolis"
}
print('Enter a state, and I will display the capital')
state = input()
x = stateCapitals.get(state)
if x==None:
print("I'm sorry, but I still need to record",state)
print('What is',state,"'s capital")
capital = input()
stateCapitals[state]=capital
print("The recorded states and capitals thus far
are:\n",stateCapitals)
else :
print(state,"'s capital is",x)