samedi 2 octobre 2021

Add and Reporting Data to/from a dictionary errors

Program has user input a State Abbreviation and then it's Capital.

If the dictionary already contains the State that the user input then it needs to report it's capital while continuing the loop to have users input States and their Capitals.

My problem is that I can't figure out how to have the program report back the Capital and then continue the loop. It just prints the state and capital infinitely.

def main():
    
    sc=  {'FL':'Tallahassee',
          'AK':'Juneau',
          'AZ':'Phoenix',
          'CA':'Sacramento',}
    count(sc)
    print("Let's add a few more")
    state=input('Enter a States Abbreviation or Enter to quit:')
   
    while state !='':
        if state in sc:
            print(f'Already have {state}. Its Capital is',sc.get(f'{state}'))
        
        if state not in sc:
            capital=input('Enter that States Capital:')
            sc[state]=capital      
            state=input('Enter a States Abbreviation or Enter to quit:')
        

          
        
    sc_len=len(sc)
    print(f'Got {sc_len} States now. Here they are...')
    for key,value in sc.items():
        print('The capital of',key,'is',value)


main()  

Aucun commentaire:

Enregistrer un commentaire