While-Loop Python Operator and/or

Hey guys, I dont understand why the operator here has to be “and” and not “or”. I thought I only use “and” if for example a user inputs more things and all have to be True and “or” if one or more of the inputs are True.

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

while choice != “y” and choice != “n”:
____choice = raw_input("Sorry, I didn’t catch that. Enter again: ")

or is used if you don’t mind either condition stopping the loop; if your case, you don’t mind if the user enters "y", or if the user enters "n", they’re both accepted inputs.

and is used when you want both conditions to be true to stop your loop, for example, if you were looping through a list of numbers, and you wanted to stop the loop only if the number was an even prime. In that case, you’d need to check if the number was even and prime, meeting just one of those criteria wouldn’t do…

I hope this helps!

1 Like