+ 1
If statement returns true despite conditional being false/not met
Hi guys, this code outputs yes no matter what I input. There seems to be a leak in the if else statement but I'm not sure what exactly. Can anyone help me with finding the problem and how I should fix it? https://code.sololearn.com/cfFe7k0wue7D/?ref=app
18 odpowiedzi
+ 4
Nessa Carter
Try the code below in the playground.
print("42" or "forty two" or "forty-two".lower().strip())
The result is "42".
So your if statement is effectively becomes:
if answer == "42"
In your writing the right part after "if answer ==" is a single expression.
It will always be True.
The correct way to write is:
if answer == "42" or answer == "forty two" or answer == "forty-two".lower().strip():
With this it breaks into 3 expressions.
You have to explicitly write each expression in coding.
Computer doesn't read sentence like human does.
+ 4
if answer== "42" or answer == "forty two" or answer == "forty-two".lower().strip():
+ 2
Hey Nessa Carter !
It will always return True, since the if condition works like:
if (condition1) and | or (condition2) and | or (conditionN)...
The if-else statement use boolean values(True or False) to work with.
Every variable with any types EXCEPT empty objects such as empty string, None object or number 0 is equivalent to True, you can check it by using the bool() function.
bool("Hello") => True
bool(None) => False
bool([1,2]) => True
bool([]) => False
The `or` operator checks if any of the condition is True, so your if-else statement works like:
if answer== "42" or "forty two" or "forty-two".lower().strip()
=> bool(answer=="42") or bool("forty-two") or bool("forty-two".lower().strip())
=> (True or False) or True or True
Since there are more than 1 True condition, it always execute.
You can fix it by making a list which contains all options and checks whether the user input matches one of the options.
Example:
n = int(input())
choice = [1,2,3]
if n in choice:
print("yes")
#[...]
+ 2
Thanks for the explanation! But does that mean I didn't define the parameters properly? I wanted it to output "yes" if the user typed in "42" or "forty-two" or "forty two" while being case insensitive and also ignoring whitespace. But when I would input something other than the above, I would still get a true. Even if the boolean value was true for all 3 of the accepted input, in the sense that all 3 had an element and weren't empty or none objects, if the input wasn't any of the 3 mentioned above, why was it considered true anyway?
+ 2
Nessa Carter ,
I don't really get it. Could you elaborate it a bit?
+ 2
Nessa Carter ,
But for checking if the object contains the given value, you can use the `in` keyword.
"Hello" in "Hello world!" => True
"ab" in "hiabc" => True
"1" in "23" => False
[1,2,3] in [[1,2,3],[4,5,6]] => True
[...]
+ 2
Nessa Carter ,
Check my previous message again..
This is how previous one works:
[ if (condition1) (and/or) (condition2) (and/or) (conditionN)... ]
if answer == "42" or "forty two" or "forty-two"
Evaluates to:
if (answer == "42") or ("forty two") or ("forty-two")
if (True/False) or True or True #Check the post I mentioned or see my previous message to know why.
Your new one:
if answer == "42" or answer == "forty two" or answer == "forty-two":
Evaluates to:
if (answer == "42") or (answer == "forty two") or (answer == "forty-two"):
if (True/False) or (True/False) or (True/False)
The if statement also checks for MULTIPLE conditions, not just one condition.
Sometimes, Programming languages' syntaxes do not obey the English rule.
+ 2
Dragon RB
I think I get you now
The error I made in my first code was that I did not put answer== in front of each accepted answer, right? So I did not correctly close the four walls and allowed any input to be considered as true, right?
thanks for your help!
+ 1
See the result with your eyes..
https://code.sololearn.com/coQGYamIeVDN/?ref=app
This page explains why it always execute better than me:
https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al
+ 1
Nessa Carter If the object isn't empty, None or 0, then it's boolean value will always be True regardless of its value..
+ 1
I understans what you are asking. People will say that you have not completed the conditions. Ex: input == condition or input == condition2 …. However, its likely related to a syntax error thats running the code through regardless of what has been entered.
0
answer = input("What is the answer?")
if answer == "42" or answer.lower().strip() == "forty two" or answer.lower().strip() == "forty-two":
print("Yes")
else:
print("No")
This code works exactly how I want it to but I'm still trying to figure out what the difference is between the previous one and this one and where I went wrong with the first one. I can't seem to find where I went wrong with the first one
0
Wong Hei Ming thanks, I realized that afterwards!
0
Nessa Carter ,
Actually, I was trying to tell you that putting only 1 value as an expression will be always True if it is not None, 0 or empty object. Suggestion: think it is better to use lists to store all possible options, and check if the list contains the user input using the `in` keyword. This will be much convenient and more readable, instead of if ans == "a" or ans == "b" etc..
0
Dragon RB
Oh okay, I'll keep that in mind
Thing is, I haven't yet learnt lists but I'll try to learn and use them soon
0
Nessa Carter
learn about truthy and falsy values.
https://www.freecodecamp.org/news/truthy-and-falsy-values-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
what is happening is your if statement is being translated to:
if answer=="42" or True or True:
which is the same as
if True:
which will always execute the print("Yes") branch.
As others have said, your conditional statement was badly formed.
0
How installo pyaudio in android