-1
ans = input("Enter yes or no: ") 


if ans != "Yes" or "yes" or "no" or "No":
    print("Can't do that") 

if ans == "yes" or "Yes": 
    print("Great!")

if ans == "no" or "No": 
    print("Okay, then") 

 

If I type let's say "Okay" it outputs this:

Can't do that! Great! Okay, then

instead of "Can't do that". I don't know what's wrong, and I couldn't find questions like this.

  • 3
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – bbnumber2 Jan 26 '21 at 19:39

5 Answers5

1

do instead something more pythonic:

if ans.lower() in ['no', 'yes']:

and use elif instead of doing another if verification.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

Use in:

ans = input("Enter yes or no: ") 

if ans in ["Yes", "yes"]:
    print("Great!")
elif ans in ["No", "no"]: 
    print("Okay, then") 
else:
  print("Can't do that")
Jakub Szlaur
  • 1,852
  • 10
  • 39
1

Test whether the answer is in a list or a set (using sets in the example below). Otherwise, your first condition evaluates to True. This is because of the operator precedence, Python considers it equivalent to (ans != "Yes") or ("yes") or ("no") or ("No"). And "yes" is True because it is not an empty string (docs), which makes the whole expression evaluate to True as well.

ans = input("Enter yes or no: ") 


if ans not in {"Yes", "yes", "no", "No"}:
    print("Can't do that") 

if ans in {"yes", "Yes"}: 
    print("Great!")

if ans in {"no" or "No"}: 
    print("Okay, then") 

Better still, make it shorter like so:

ans = input('Enter yes or no: ').lower() 

if ans == 'yes': 
    print('Great!')
elif ans == 'no': 
    print('Okay, then')
else:
    print("Can't do that") 
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
0

That's not the way to use logical operators in a programming language, it should be:

if (ans != "Yes") or (ans != "yes") or (ans != "no") or (ans != "No")

As you see, you should always repeat your variable.

Dominique
  • 16,450
  • 15
  • 56
  • 112
-1

the or operator assignment is not right please try this

ans = input("Enter yes or no: ")

if ans != "Yes" or "yes" or "no" or "No": print("Can't do that")

if ans == "yes" or "Yes": print("Great!")

if ans == "no" or "No": print("Okay, then")

you can either write if ans in ('stringone', 'stringtwo'): dosomething()

Or you can write separate equality tests,

if var == 'stringone' or var == 'stringtwo':
    dosomething()