Page 381 - Computer Science Class 11 With Functions
P. 381

11         stateCapital[state] = capital
              12         state = input('Enter state:')
              13         capital = input('Enter capital:')
              14     return stateCapital
              15
              16
              17 def searchCapital(myDict, state):
              18     '''

              19     Input Parameters: myDict - Dictionary containing state as keys and capital as values
              20                      state - state for which capital is to be determined
              21
              22     Return Value:
              23          capital, if state is present as key in the dictionary
              24          updated dictionary with new entry for the state and capital, otherwise
              25     '''
              26     if state in myDict:
              27         print('Capital of', state, 'is',myDict[state])
              28     else:
              29         print('No record found for given state!')
              30         print('Would you like to provide its capital to be added to dictionary? (Y/y)')
              31         choice = input('')
              32         if choice in ('Y', 'y'):
              33             capital = input('Enter the capital of '+state+': ')
              34             myDict[state] = capital
              35             print(myDict)
              36
              37
              38 def searchState(myDict, capital):
              39     '''

              40      Input Parameters: myDict - Dictionary containing state as keys and capital as values
              41                       capital - capital to which state is mapped is to be determined
              42     Return Value:
              43          state, if capital occurs in any of the key-value pair
              44     '''
              45     states = set()
              46     for state in myDict.keys():
              47         if capital == myDict[state]:
              48             states.update(set([state]))
              49     if states == set():
              50         states = 'No States found'
              51     else:
              52         print('States with capital', capital, ':', states)
              53
              54
              55 myDict = stateCapitalDict()
              56 print(myDict)
              57 state = input('Enter a state: ')
              58 searchCapital(myDict, state)
              59 capital = input('Enter a capital: ')
              60 searchState(myDict, capital)
            Sample output:

                  Enter state:Bihar
                  Enter capital:Patna
                  Enter state:Haryana
                  Enter capital:Chandigarh
                  Enter state:Jharkhand
                  Enter capital:Ranchi



                                                                                                   Dictionaries  379
   376   377   378   379   380   381   382   383   384   385   386