How can I check for two conditions at once?



How can I check for two conditions at once?


If we want two things to be True before entering a loop, we can use the and operator. Recall that and requires both sides to be True for the entire condition to be True, otherwise it is False.
We’d write condition_1 and condition_2, where condition_1 is the choice not being "y", and condition_2 is the choice not being "n",

FAQ: Learn Python- Loops - Simple errors

But why is there an “and” operator needed in that exercise?
Wouldn’t a logical “and” not insist that both answers are true? Like ‘y’ and ‘n’ would mean that there have to be a ‘y’ AND an ‘n’. I am a bit confused :confused:


yes, but you check if the user input does not equal y and n

if the user enters something invalid (like x), both condition are true, True and True is true, so the loop will run again. Which should happen, given we need to get new input

if the user enters valid input (for example y) one of the conditions is True while the other is False. True and False is false, so the loop stops (which it should, we have valid input)

on the other hand, using or will always result in true (True or True, True or False both evaluate to true), so the loop will keep running forever


Because if you press “y”, you are not pressing “n”… so it will result to an error


Hey guys, thank you very much for your rapid answers.
Now I noticed the failure in my thinking.
Best Regards