Page 218 - Information_Practice_Fliipbook_Class11
P. 218
07 is prompted to add the capital of the newly entered state.
08 (3) Provide mapping of capital to its state
09 '''
10
11 stateCapital = dict()
12 state = input('Enter state:')
13 capital = input('Enter capital:')
14 while state != '' and capital != '':
15 stateCapital[state] = capital
16 state = input('Enter state:')
17 capital = input('Enter capital:')
18
19 state = input('Enter a state for which the capital needs to be determined: ')
20
21 if state in stateCapital:
22 print('Capital of', state, 'is',stateCapital[state])
23 else:
24 print('No record found for given state!')
25 print('Would you like to provide its capital to be added to the
26 dictionary? (Y/y)')
27 choice = input('')
28 if choice in ('Y', 'y'):
29 capital = input('Enter the capital of '+state+': ')
30 stateCapital[state] = capital
31 print(stateCapital)
32
33 capital = input('Enter a capital for which the state needs to be
34 determined:')
35
36 states = set()
37 for myState, myCapital in stateCapital.items():
38 if capital == myCapital:
39 print('State with capital', capital, ':', myState)
Sample output:
>>> Enter state:Bihar
>>> Enter capital:Patna
>>> Enter state:Haryana
>>> Enter capital:Chandigarh
>>> Enter state:Jharkhand
>>> Enter capital:Ranchi
>>> Enter state:Manipur
>>> Enter capital:Imphal
>>> Enter state:Tripura
>>> Enter capital:Agartala
>>> Enter state:
>>> Enter capital:
>>> Enter a state for which the capital needs to be determined: Bihar
Capital of Bihar is Patna
>>> Enter a capital for which the state needs to be determined:Imphal
State with capital Imphal : Manipur
Let's Summarise
Ø A dictionary is an unordered set of key: value pairs.
Ø A dictionary may be specified by enclosing the key: value airs in braces {}.
204 Touchpad Informatics Practices-XI

