Simple errors: while not in ["y", "n"] doesnt work


#1

https://www.codecademy.com/en/courses/learn-python/lessons/loops/exercises/simple-errors

Hi all. The code works fine but is not accepted as answer, but I thin it should. Or how am I wrong?

choice = raw_input('Enjoying the course? (y/n)')
while not choice in ["y","n"]:  # Fill in the condition (before the colon)
  choice = raw_input("Sorry, I didn't catch that. Enter again: ")
Does your code accept anything other than "y" and "n"?

#2

Precedence means not will be evaluated first. not choice will be False assuming there is some input.

choice not in

Now choice is not evaluated by not, but in [...] is.


#3

oh, of course, thanks :slight_smile:


#4

counter-example:

>>> not 'a' in 'b'
True
>>> (not 'a') in 'b'
TypeError, or False if ['b'] is used instead

https://docs.python.org/reference/expressions.html#operator-precedence

@hilfe-eine-schlange
Your code is fine (though I’d prefer the not in operator)
The submission test doesn’t test the behaviour of your code in this case, instead it looks at the text which is evidently not reliable.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.