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:
JavaScript
x
21
21
1
# asking the bro
2
are_you_happy = input("You feel bad bro? ")
3
4
# psicological support loop haha
5
while are_you_happy.upper() == "YES" or "YEAH":
6
print("*give him a hug*")
7
better = input("you feel better bro? ")
8
9
# he feel better boys :)
10
if better.upper() == "YES" or "YEAH":
11
print("i'm happy for you")
12
break
13
14
# he don't feel better :(
15
elif better.upper() == "NO":
16
more_support = input("do you want another hug? ")
17
if more_support.upper() == "YES" or "YEAH":
18
print("*give him a very huge hug")
19
print("i hope you well bro")
20
break
21
Advertisement
Answer
In Python, the expression:
JavaScript
1
2
1
something == "YES" or "YEAH"
2
is equivalent to:
JavaScript
1
2
1
(something == "YES") or ("YEAH")
2
And, since "YEAH"
is a truthy value, this will always be true. The right way to use or
in this case is with:
JavaScript
1
2
1
something == "YES" or something == "YEAH"
2
but you would be better off with the more Pyhtonic:
JavaScript
1
2
1
something in ["YES", "YEAH"]
2
Make sure you change all three of the problem lines in your code.