If the user input == a key of a dictionary then return the value and vice versa. how could I do that?

Question:

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?

Asked By: Aharon Becker

||

Answers:

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]
Answered By: Michael Hodel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.