Not clear on line 03

Here, I can’t figure out why line three will fail to find the rooms. I’ve tried trust me.

Thank You!

Where are you getting this test? You’re the 3rd or 4th person who’s been showing this super weird online Python quiz that has really wrong questions. I’d like to go yell at someone for these.

Assuming this is Python 3, then input will return a string, which means this code is just wrong. The keys to this dict are numbers, so it will produce an error:

Python 3.6.3 (default, Dec  7 2017, 00:40:34)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = {1: "Mayer", 2: "Conference Room"}
>>> room = input("What room?")
What room?1
>>> room
'1'
>>> type(room)
<class 'str'>
>>> room in x
False
>>>

So it won’t find it because input() returns a string in Python 3. However in Python 2:

Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = {1: "Mayer", 2: "Conference Room"}
>>> room = input("What room?")
What room?1
>>> type(room)
<type 'int'>
>>> x in room
True
>>>

But, nobody should be using input() in Python 2 because it’s a massive security hole. Normally I don’t care about that too much with beginners, but in the case of input() in python 2 it’s important enough that nobody should be teaching input() at all.

So, the answer to the third question can be:

  1. Because in Python 3 input returns a string but the keys are integers. If that’s the answer then the code is wrong and the answer to the second question is wrong.
  2. If it’s Python 2 then the only way it would happen is if the user just types in the wrong answer, as it would look it up as an int.

Actually this question is was on the exam? And we are using Python 3 with your book Python 3 The Hard Way.

Ok, well this question is not very good. I’m assuming the answer is because input() returns a string, but the keys are integers. You’d need to do int(input()) to get the right one.