Skip to content
Advertisement

The else function does not appear even if the “if” conditions are not met [closed]

The problem is when the bro says “no” the condition “if” always appears even if i use “else” or “elif”, why? Isn’t it supposed that if the input is something other than yes or yeah, is else what appears? pls help

ok here is my code:

# asking the bro
are_you_happy = input("You feel bad bro? ")

# psicological support loop haha
while are_you_happy.upper() == "YES" or "YEAH":
  print("*give him a hug*")
  better = input("you feel better bro? ")

  # he feel better boys :)
  if better.upper() == "YES" or "YEAH":
    print("i'm happy for you")
    break

  # he don't feel better :(
  elif better.upper() == "NO":
    more_support = input("do you want another hug? ")
    if more_support.upper() == "YES" or "YEAH":
        print("*give him a very huge hug")
        print("i hope you well bro")
        break

enter image description here

Advertisement

Answer

In Python, the expression:

something == "YES" or "YEAH"

is equivalent to:

(something == "YES") or ("YEAH")

And, since "YEAH" is a truthy value, this will always be true. The right way to use or in this case is with:

something == "YES" or something == "YEAH"

but you would be better off with the more Pyhtonic:

something in ["YES", "YEAH"]

Make sure you change all three of the problem lines in your code.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement