If statement returns true despite conditional being false/not met | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

22nd Oct 2023, 11:41 AM
Nessa Carter
18 Answers
+ 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.
22nd Oct 2023, 2:47 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
if answer== "42" or answer == "forty two" or answer == "forty-two".lower().strip():
22nd Oct 2023, 4:24 PM
Angela
Angela - avatar
+ 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") #[...]
22nd Oct 2023, 12:25 PM
Dragon RB
Dragon RB - avatar
+ 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?
22nd Oct 2023, 1:27 PM
Nessa Carter
+ 2
Nessa Carter , I don't really get it. Could you elaborate it a bit?
22nd Oct 2023, 1:47 PM
Dragon RB
Dragon RB - avatar
+ 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 [...]
22nd Oct 2023, 1:55 PM
Dragon RB
Dragon RB - avatar
+ 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.
22nd Oct 2023, 2:04 PM
Dragon RB
Dragon RB - avatar
+ 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!
22nd Oct 2023, 4:46 PM
Nessa Carter
22nd Oct 2023, 12:33 PM
Dragon RB
Dragon RB - avatar
+ 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..
22nd Oct 2023, 1:51 PM
Dragon RB
Dragon RB - avatar
+ 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.
22nd Oct 2023, 11:03 PM
Andre Richmond
Andre Richmond - avatar
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
22nd Oct 2023, 1:58 PM
Nessa Carter
0
Wong Hei Ming thanks, I realized that afterwards!
22nd Oct 2023, 4:51 PM
Nessa Carter
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..
22nd Oct 2023, 5:09 PM
Dragon RB
Dragon RB - avatar
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
22nd Oct 2023, 5:13 PM
Nessa Carter
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.
23rd Oct 2023, 9:27 PM
Bob_Li
Bob_Li - avatar
23rd Oct 2023, 9:30 PM
شادي الحربي
0
How installo pyaudio in android
24th Oct 2023, 6:01 AM
salimo saide
salimo saide - avatar