I’m reading a book on beginner’s python programming…can someone tell me why this simple program isn’t working?

I’m reading a book on beginner’s python programming…can someone tell me why this simple program isn’t working?
0

#1
me = {
    "height": "6",
    "fav_color": "red",
    "fav_author": "Orwell"
}

answer = input("Type height, fav_color or fav_author")
if answer in me:
    result = me[answer]
print(result)

#2

Python requires indentation. I do not see any.


#3

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


#4

What about this doesn’t seem to be working for you?
When you input answer are you typing ‘height’, ‘fav_color’, or ‘fav_author’ literally? Or are you entering ‘6’, ‘red’, or ‘Orwell’?


#5

The reason is, dictionary searches only in keys normally.
So you should either enter the input as -> “height” , “fav_color” or “fav_author”.

Don’t forget to enclose the input in quotes ( " ). Python 3 expects you to give the input as it is.
If it is a string you should enclose it in quotes.


#6

On the first premise, it kinda depends on whether they’re attempting to search keys or values.
I’m pretty sure your second premise is not true. To my knowledge, Python 3 reads a line from input, converts it to a string and returns that (this is why you have to wrap the input in int() or float() when input is a number). So if you wrap the input in quotes you’re going to get an extra set of quotes around the answer and it’s never going to be found in the dictionary search:

>>> answer = input("Type height, fav_color or fav_author: ")
Type height, fav_color or fav_author: "fav_author"
>>> answer
'"fav_author"'

As opposed to:

>>> answer = input("Type height, fav_color or fav_author: ")
Type height, fav_color or fav_author: fav_author
>>> answer
'fav_author'