Why is "or" a no-go? -- "Simple Errors"


#1



https://www.codecademy.com/en/courses/python-beginner-en-cxMGf/0/4?curriculum_id=4f89dab3d788890003000096

I originally used an "or" in place of the "and" in this line, and it doesn't recognize a y or an n as valid replies -- although the code does pass!

It seems to me that typing in a y "or" an n should satisfy the condition. In fact, "and" doesn't make sense to me, because I'm only inputting one letter. Below is my code with the "and" that passes.


[spoiler]choice = raw_input('Enjoying the course? (y/n)')
y = "y"
n = "n"
#print choice
while choice != y and choice != n:
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")
    break[/spoiler]


#2

agree the loops keeps running while the condition is true?

if the user enters something invalid (lets say x), we would get the following:

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

both conditions are true:

true or true

which is true, the loop will make another iteration. Now, if the user enters something valid:

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

which results in:

false or true

which is true, the loop will make another iteration.

this is why we use and, because false and true is false, causing the loop to end


Simple Errors
#3

Thank you for your help -- I forgot that we're testing for True or False in order to evaluate the variable, rather than testing the variable directly.

Incidentally, I had considered this before you replied, and I thought of deleting the topic. But I could not figure out how. Is it possible to delete a topic after creating it?


#4

conditions always evaluate to True or False, there is no other possibility.

You can't, given topics might contain useful content of other learners.


#5

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