Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using or in python

Tags:

python

syntax

New to coding, having problems making foolproof input for program:

answer=input(x)
while (answer != "yes") or (answer != "no"):
    answer=input("must be 'yes' or 'no' answer")

The while loop never stops, regardless of input. What am I doing wrong?

like image 470
user2998454 Avatar asked Jan 27 '26 02:01

user2998454


2 Answers

Here is what I would use:

while answer not in ("yes", "no"):

Right now, your code is running continually because answer will always be not "yes" or not "no".


Also, if you want, you can add .lower() like so:

while answer.lower() not in ("yes", "no"):

This will allow your code to handle inputs such as "Yes", "yEs", etc.

Everything is either not yes or not no. You want and.

like image 25
BrenBarn Avatar answered Jan 29 '26 14:01

BrenBarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!