Page 331 - Computer Science Class 11 Without Functions
P. 331

04            capital as values
              05        (2) Searches for a capital for the user entered state. If the
              06            entered state is not present in the dictionary, the user
              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)
            Snmpli 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










                                                                                             Python Dictonnaiie  329
   326   327   328   329   330   331   332   333   334   335   336