2

I've just started using python and have got stuck on something that, in my mind, clearly should work. This is my first code and I just try to have a conversation with the user.

year = input("What year are you in school? ")
yearlikedislike = input("Do you like it at school? ")
if (yearlikedislike == "yes" or "Yes" or "YES" or "yep" or "yup" or "Yep" or "Yup"):
    print("What's so good about year " + year, "? ")
    input("")     
    print("That's good!")
    time.sleep(1)
    endinput = input("I have to go now. See you later! ")
    exit()
if (yearlikedislike == "no" or "No" or "nope" or "Nope" or "NOPE"):
    print("What's so bad about year " + year, "?")
    input("")
    time.sleep(1)
    print("Well that's not very good at all")
    time.sleep(1)
    endinput = input("I have to go now. See you later! ")
    time.sleep(1)
    exit()

My problem is that even if I reply with a negative answer it will still reply with a response as if I have said yes and if I switch the 2 around (so the code for the negative answer is above the code for the positive answer) it will always reply as if I have given a negative response.

Matthew Asker
  • 31
  • 1
  • 3

4 Answers4

6
if yearlikedislike in ("yes", "Yes", "YES", "yep", "yup", "Yep", "Yup"):

or

if yearlikedislike.lower() in ("yes","yep","yup"):

will do the trick

Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
mhlester
  • 22,781
  • 10
  • 52
  • 75
2

This is because Python is evaluating the "truthiness" of "Yes".

Your first if statement is interpreted like this:

if the variable "yearlikedislike" equals "yes" or the string literal "Yes" is True (or "truthy"), do something

You need to compare against yearlikedislike each time.

Try it like this:

if yearlikedislike in ("yes", "Yes", "YES", "yep", "yup", "Yep", "Yup"):
    #do something
That1Guy
  • 7,075
  • 4
  • 47
  • 59
2
if (yearlikedislike == "yes" or "Yes" or "YES" or "yep" or "yup" or "Yep" or "Yup"):

Strings evaluate to True. I know you think you're saying that if yearlikedislike is equal to any of those things, keep going. However, what you're actually saying is:

if yearlikedislike equals "yes", or if "Yes" exists (which it does), or "YES" exists, etc:

What you want is either:

if (yearlikedislike == "yes" or yearlikedislike == "Yes" or yearlikedislike == "YES")

or better:

yearlikedislike in ("yes", "Yes", "YES", "yep", "yup", "Yep", "Yup")
thumbtackthief
  • 6,093
  • 10
  • 41
  • 87
1

It is because the condition is interpreted as :

if(yearlikedislike == "yes" or "Yes" == True or "YES" == True #...

try

if(yearlikedislike == "yes" or yearlikedislike == "Yes" or yearlikedislike == "YES"#...

or a more concise way :

if(yearlikedislike in ("yes", "Yes", "YES", #...

even more concise way :

if(yearlikedislike.lower() in ("yes", "yup", #...

A String (here "Yes") converted to a boolean is converted to True if it's not empty

>>> bool("")
False
>>> bool("0")
True
>>> bool("No")
True

Each part after or is independant from the previous.

Also consider using else or elif instead of two related if. And try to lower character before testing them so you need less test.

luxcem
  • 1,807
  • 22
  • 37