4/19 - while choice != "y" or choice != "n": is this wrong?


#1

It keeps looping even when I type in y or n. But as far as I am aware, my if statement says that it will loop only when my input is NOT y OR n… what did I do wrong?

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

while choice != "y" or choice != "n":  # Fill in the condition (before the colon)
  choice = raw_input("Sorry, I didn't catch that. Enter again: ")

Edit: also when I run it, I get this in my console before I even enter anything:

Sorry, I didn’t catch that. Enter again: Sorry, I didn’t catch that. Enter again: Sorry, I didn’t catch that. Enter again: Sorry, I didn’t catch that. Enter again: Sorry, I didn’t catch that. Enter again: Sorry, I didn’t catch that. Enter again: Sorry, I didn’t catch that. Enter again: Sorry, I didn’t catch that. Enter again:


#2

there are two possible scenarios, valid input (y or n) and invalid input by the user

lets do invalid input first, lets say the user enters x we get:

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

which results in:

while True or True:

True or True is True, so that is good, given we want to keep the loop running

what if the enters valid input, for example y:

while 'y' != 'y' or 'y' != 'n':

If we evaluate the condition we get:

while False or True:

False or True evaluates to True, so the loops keep running. Oops, that can’t be right? The user entered valid input, the loop should exit.

loop run for as long as the condition evaluates to True


#3

Oh no! Ahhh, thanks so much for the detailed explanation.


#4

Yep, but the most valuable lesson for you shouldn’t be that you now understand the problem

the most important lesson should be to learn to break the problem down into steps like i just did, its a very valuable skill. Not that we don’t like helping on the forum, but programming is mostly problem solving, so that is what you should learn :slight_smile:


#5

I look at this for about 1.5 seconds and after that I’ll give up on trying to understand it and rewrite it first.

while not (choice == "y" and choice == "n")

There’s even a fancy name for this particular case, De Morgan’s laws, but that doesn’t really matter. What does matter is that you’re not going to want multiple NOT’s in your expression because it’s unreadable unless you bend your mind over backwards. It’s the same thing as how you should be avoiding double negatives in human language, people won’t keep track of it.

Obviously choice won’t be equal to two differing values at the same time, so that expression isn’t useful.

A step further on the rewriting of that (while also correcting it)

while choice not in 'yn'

But yeah, sure, you can give it a mathematical eye, pick it apart or something. I’d rather have code be more like natural language if at all possible, allowing for just reading things without strain, always trying to write things as obvious as possible.


#6

Thanks! Whoa, I didnt know that I could type out “while choice not in ‘yn’”

Thanks so much guys. I am really enjoying your insights. I’m learning so much everyday.


#7

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