Home > Back-end >  If the user input == a key of a dictionary then return the value and vice versa. how could I do that
If the user input == a key of a dictionary then return the value and vice versa. how could I do that

Time:08-22

I want to make a program, where in a dictionary, I put in peoples names and their email addresses, and when the user puts in a name it gives back the email. Is their a quicker way to do that other than doing a whole lot of if loops or maybe a for loop? Any ideas?

CodePudding user response:

d = {'Alice': '[email protected]', 'Bob': '[email protected]'}
user_input = input('Input a name:').lower().capitalize()
if user_input in d:
    print(d[user_input])

If inputting Bob, (irrespective of capitalization), this prints

Input a name:Bob
[email protected]
  • Related