I am writing a script that prompts the user for a state name. the script checks if the state is actually in the dictionary and gives back the capital. If the state does not in the dic, it states that isn't in the dictionary. I tried try except with KeyError but Python still gives me an error. What did I do wrong?
state_dictionary = {'Colorado': 'Denver', 'Alaska': 'Juneau', 'California': 'Sacramento',
'Georgia': 'Atlanta', 'Kansas': 'Topeka', 'Nebraska': 'Lincoln',
'Oregon': 'Salem', 'Texas': 'Austin', 'New York': 'Albany'}
if True:
search_state=input("What is the State of the Capital you want to know? ")
try:
state_dictionary[search_state]
except KeyError:
print ('No such a State')
print (state_dictionary[search_state])
What is the State of the Capital you want to know? Italia
No such a State
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-38-c6d25bb66225> in <module>()
13 except KeyError:
14 print ('No such a State')
---> 15 print (state_dictionary[search_state])
KeyError: 'Italia'