4. Simple Errors


#1

Hi, I understand the correct answer for this one but could someone tell me why these methods dont work?

choice = raw_input('Enjoying the course? (y/n)')

while choice != ("y" or "n"):
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")

or

choice = raw_input('Enjoying the course? (y/n)')

while choice == (not ("y" or "n")):
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")

in the first, "y" ends the loop and "n" does not. In the second, any input ends the loop.


#2

Both loops works fine? The exercise just want you to use two != in order to pass the exercise.


#3

That's confusing, I've just tried them and they don't work for me


#4

I ran both, and they both worked:

Ran both in the exercise as well, also no problem... Weird....


#5

Inputting "y" does work for both. but for the 1st one "n" doesn't work. For the second everything ends the loop even if it isn't y or n.


#6

Oh wow, i missed that sentence completely. In that case, lets break down the loops.

First off, python doesn't have the feature which allows you to this:

("y" or "n")

you really need to check both conditions:

while choice != "y" or  choice != "n":

as nicely explained here. Now, you should use and, not or. Why?

Agree with me that a loop runs as long the condition is true? The moment the user enters something invalid (like x), both condition are true (x does not equal y, true. x does not equal n, true), the loops keeps running as it should. But what if the user enters something valid like y? Then y does not equal y is false, and y doesn't equal n is true. So then we have: False or True, which will be True, and the loop keeps running

Now, let's use and instead. If the user enters something invalid, both conditions are true. (see above), of the user enters something valid (For example n), you get: n does not equal y, which is true. n does not equal n, which is false. So now we have: True and False. True and False is False, the loop ends, as it should, since the user enters something valid

With this knowledge you should be able to solve the second loop as well


#7

Thank you for the reply, however I already understand the second part. But even after reading your link I can't seem to understand why the loop using...

("y" or "n")

...returns false when you input "n" or any other character, but not when you input "y".

Is it like, it evaluates the statement on it's own and resolves to "y" since it is the first "truthy" value? and only then compares that to the input?


#8

because the brackets don't matter. the statement you have is:

if choice !== 'y' or 'n'

So, the y gets validated properly, but the 'n' doesn't because you don't compare it to anything, it should be:

if choice !== 'y' or choice !== 'n'

with our without brackets, it doesn't matter.


#9

I see. Thanks for the help


#10

Shouldn't there be and instead of or?


#11

Thank you sherlock, yes there should be, which (if you read the whole thing) i am explaining


#12

I was asking, just to make sure I understand.


#13

But i already explained it, but yes, you understand it correctly, that should be and


#14

very good answer thanks