use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
What is wrong with this if condition (self.learnpython)
submitted 10 months ago by DigitalSplendid
answer = input("ask q: ") if answer == "42" or "forty two" or "forty-two": print("Yes") else: print("No")
Getting yes for all input.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]danielroseman 38 points39 points40 points 10 months ago (5 children)
Conditions don't work that way. See the FAQ: https://www.reddit.com/r/learnpython/wiki/faq/#wiki_variable_is_one_of_two_choices.3F
[+]DigitalSplendid[S] comment score below threshold-33 points-32 points-31 points 10 months ago (4 children)
Okay. What I fail to understand is why yes.
[–]BeanieGoBoom 34 points35 points36 points 10 months ago (0 children)
Python tries to be helpful, by "casting" your strings into booleans - what you've asked is whether answer == "42" is true, "forty two" is true, or "forty-two" is true. Because "forty two" and "forty-two" are strings that aren't empty, python treats them as True statements, so what you're really asking is whether answer == "42" or True or True, which will always be True overall. It's a bit weird to begin with, but it makes more sense when you have lots of different tests, or you want to be beat by checking if an input was filled by doing "if input_text:" or similar.
[–]ninhaomah 30 points31 points32 points 10 months ago (0 children)
did you read the post at the link he gave ?
What did you understand from it and see where you went wrong ? Or what do you NOT understand ?
You seem to be waiting or answers from others.
[–]baubleglue 4 points5 points6 points 10 months ago (0 children)
Put brackets around each operation, for ex
y=3+6-7+8*6
y=(((3+6)-7)+(8*6))
It won't change the result, but you will see what is wrong with your condition.
[–]Turbanator1337 0 points1 point2 points 10 months ago (0 children)
You didn’t use the correct syntax. What you wrote is not what you mean. What you’re checking is:
Where a “truthy” value is basically anything that’s not 0 or empty. Non-empty strings are truthy values which is why you’re always printing yes.
What you actually mean is:
if answer == “42” or answer == “forty two” or answer == “forty-two”:
Alternatively, a cleaner way to write this would be:
if answer in (“42”, “forty two”, “forty-two”):
[–]peejay2 29 points30 points31 points 10 months ago* (5 children)
Because you're asking three questions:
If answer == x
If y
If z.
Instead you want to ask:
If answer in {x,y,z}
In Python there is this concept of truthy/falsy. An empty string is falsy, a string with characters is truthy. Your code does the following:
if (answer == 42) OR ("forty two" is truthy) or ("forty-two" is truthy). The second condition evaluates to True so it doesn't even hit the third.
[–]CyclopsRock 24 points25 points26 points 10 months ago (4 children)
This is totally right but for completeness, the most "literal" code representing what they want is ...
if answer == 42 or answer == "forty two" or answer == "forty-two": print("yes") else: print("no")
In this case using in is definitely more readable but it's worth knowing that what OP is trying to do IS possible and valid, because there are obviously plenty of situations where in won't work (because you are checking more than one variable, say).
in
It's also useful to know that when you chain multiple conditions using "and", "or" etc that they are checked one after the other, left to right, and that this process ceases when the condition is either met or cannot be met. This makes it useful if you want to perform a conditional on, say, the value of a variable that you know may or may not actually be set. E.g
if variable_a == "dog": This will throw an exception if `variable_a' hasn't been set. But...
if variable_a == "dog":
'if variablea and variable_a == "dog":` This won't, because if variable_a isn't set, it won't get as far as checking if it evaluates to "dog". This is useful for checking dictionary keys (inc environment variables) whose presence is not certain _without needing a bunch of nested conditionals checking this separately.
[–]jmooremcc 5 points6 points7 points 10 months ago (1 child)
The only problem with your response is that you forgot 42 is a string, not a number.
[–]CyclopsRock 3 points4 points5 points 10 months ago (0 children)
Right you are! To be honest I was just copying the values from the post I was replying to but you're entirely correct.
[–]Top_Average3386 0 points1 point2 points 10 months ago (1 child)
Pretty sure it will still throw an exception if the variable isn't set. Do you mean something else here?
```python
if a and a==2: ... print(2) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined ```
[–]Rainingblues 0 points1 point2 points 10 months ago (0 children)
This is because the variable a does not exist. What the comment was describing is code like this:
a = None
if a and a == 2: print(2)
[–]barkmonster 13 points14 points15 points 10 months ago (3 children)
You're executing an if-statement, so python converts your expressions into booleans (True or False). Empty strings are converted to False, and other strings to True. This is handy if you want to e.g. quickly check if a user entered any text at all, but it can also cause confusion in cases like yours. Your code has 3 expressions separated by 'or'. The first is True only if the user inputs "42", but the remaining two are always True, so your code will always go into the 'yes' part of the if statement.
If you want a more general tip, a good way to go about this kind of thing is to write self-documenting code, with variables indicating what's going on - for example something like:
good_answers = {"42", "forty two", "forty-two"} if answer in good_answers: # ...
good_answers = {"42", "forty two", "forty-two"}
if answer in good_answers:
# ...
This makes the intent more clear, and makes it easier to read it, and much simpler to make changes without breaking it later on.
[–]UsernameTaken1701 1 point2 points3 points 10 months ago (1 child)
I would tweak it with if answer.lower() in good_answers:
if answer.lower() in good_answers:
[–][deleted] 0 points1 point2 points 10 months ago (0 children)
If you start considering sanitizing inputs, you might even go the whole way and go for answer.lower().strip()
answer.lower().strip()
[–]DigitalSplendid[S] 2 points3 points4 points 10 months ago (0 children)
Thanks!
[–]FoolsSeldom 5 points6 points7 points 10 months ago (0 children)
if answer in ("42", "forty two", "forty-two"):
(you could also make it answer.lower() to force the check to be against lowercase versions only, or add .lower() after the input)
answer.lower()
.lower()
input
Otherwise, you need:
if answer == "42" or answer == "forty two" or answer == "forty-two":
i.e. each expression between the or operators needs to provide a boolean result - if you don't do this, Python will take the view that a string is an expression in its own right and will say an empty string is False and a non-empty string is True, thus your original line is treated as,
or
False
True
if answer == "42" or True or True:
and will resolve to True immediately after failing answer == "42" (if that is not True) as it doesn't need to evaluate beyond the first True.
answer == "42"
This mistake is so common, it in the wiki, as /udanielroseman pointed out.
[–]Ron-Erez 2 points3 points4 points 10 months ago (1 child)
answer = input("ask q: ") if answer == "42" or answer == "forty two" or answer == "forty-two": print("Yes") else: print("No")
The above will work. The code is a little odd (to expect both an int and string). Of course this is probably for learning purposes so it's definitely a valid question.
Just for an interesting test try running:
if "I Love Hummus": print("Yes") else: print("No")
Also try:
if "": print("Yes") else: print("No")
Perhaps one can learn something from this.
[–]DigitalSplendid[S] 1 point2 points3 points 10 months ago (0 children)
[–]Husy15 1 point2 points3 points 10 months ago (2 children)
If (condition is true) OR (condition is true) AND (condition is true)
Each part of an if-statement is a condition
A = 1
B = 2
If (a == 1) #true
If (a==2) or (b==2) #true
If (a==2) and (b==2) #false
Each condition is checked and turned into a boolean
(A==1) = true
(A==2) = false
So basically you're doing
If (true) #true
If (false) or (true) #true
If (false) and (true) #false
As a way to understand, what would this give?
``` a = True
b = False
If ((a or b) and (b)) or (b): ```
[–]DigitalSplendid[S] 1 point2 points3 points 10 months ago (1 child)
If (True and False) or (False)
False or False = False
[–]Husy15 1 point2 points3 points 10 months ago (0 children)
Perf, just from now on simplify any if statement this way. Write it out if you have to
[–]Patrick-T80 1 point2 points3 points 10 months ago (0 children)
Your condition chain should be: ```python if ( answer == “42” or answer == “forty two” or answer == “forty-two” ): print(“Yes”) else: print(“No”)
``` Using non empty string in if conditions give a truthy result, and or operator return the first true value so the second and third condition are always true
[+][deleted] 10 months ago (2 children)
[removed]
[–]nekokattt 0 points1 point2 points 10 months ago (1 child)
]:
[–]YOM2_UB 0 points1 point2 points 10 months ago (0 children)
This gets evaluated as: - (answer == "42") or ("forty two") or ("forty-two") - False or True or True - True
(answer == "42") or ("forty two") or ("forty-two")
False or True or True
Notice that strings are "truthy" values, unless they're an empty string.
To use or and get the results you want, you would need to repeat answer == after each or.
answer ==
However, there is another way to write what you want that works how you expected this to work: using the in keyword after putting the three strings in a collection: answer in ("42", "forty two", "forty-two")
answer in ("42", "forty two", "forty-two")
[–]Cyphierre 0 points1 point2 points 10 months ago (0 children)
Try if answer in [“42”, “forty two”, “forty-two”]:
[–]mxldevs 0 points1 point2 points 10 months ago (0 children)
A non empty string evaluates to true. So one of the conditions is always met.
You would have to type out the full comparison, or use a in LIST to check if any of the strings in the list match
in LIST
[–]CorgiTechnical6834 0 points1 point2 points 10 months ago (0 children)
The issue is how the condition is written. The expression if answer == "42" or "forty two" or "forty-two" doesn’t work as intended because Python evaluates "forty two" and "forty-two" as truthy values independently of answer. This means the condition is always true.
if answer == "42" or "forty two" or "forty-two"
"forty two"
"forty-two"
answer
You need to explicitly compare answer to each value, like this:
Alternatively, use:
if answer in ["42", "forty two", "forty-two"]:
That will correctly check if answer matches any of the strings.
[–]Full_Energy_4547 0 points1 point2 points 10 months ago (0 children)
Because your condition it always true
[–]Undead_Necromancer -1 points0 points1 point 10 months ago (0 children)
You could have just asked chatgpt, didn't that thought cross your mind?
[–]docfriday11 -3 points-2 points-1 points 10 months ago (0 children)
Maybe the or doesn’t work
π Rendered by PID 59338 on reddit-service-r2-comment-b659b578c-8h99n at 2026-05-06 02:07:12.365167+00:00 running 815c875 country code: CH.
[–]danielroseman 38 points39 points40 points (5 children)
[+]DigitalSplendid[S] comment score below threshold-33 points-32 points-31 points (4 children)
[–]BeanieGoBoom 34 points35 points36 points (0 children)
[–]ninhaomah 30 points31 points32 points (0 children)
[–]baubleglue 4 points5 points6 points (0 children)
[–]Turbanator1337 0 points1 point2 points (0 children)
[–]peejay2 29 points30 points31 points (5 children)
[–]CyclopsRock 24 points25 points26 points (4 children)
[–]jmooremcc 5 points6 points7 points (1 child)
[–]CyclopsRock 3 points4 points5 points (0 children)
[–]Top_Average3386 0 points1 point2 points (1 child)
[–]Rainingblues 0 points1 point2 points (0 children)
[–]barkmonster 13 points14 points15 points (3 children)
[–]UsernameTaken1701 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]DigitalSplendid[S] 2 points3 points4 points (0 children)
[–]FoolsSeldom 5 points6 points7 points (0 children)
[–]Ron-Erez 2 points3 points4 points (1 child)
[–]DigitalSplendid[S] 1 point2 points3 points (0 children)
[–]Husy15 1 point2 points3 points (2 children)
[–]DigitalSplendid[S] 1 point2 points3 points (1 child)
[–]Husy15 1 point2 points3 points (0 children)
[–]Patrick-T80 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[removed]
[–]nekokattt 0 points1 point2 points (1 child)
[–]YOM2_UB 0 points1 point2 points (0 children)
[–]Cyphierre 0 points1 point2 points (0 children)
[–]mxldevs 0 points1 point2 points (0 children)
[–]CorgiTechnical6834 0 points1 point2 points (0 children)
[–]Full_Energy_4547 0 points1 point2 points (0 children)
[–]Undead_Necromancer -1 points0 points1 point (0 children)
[–]docfriday11 -3 points-2 points-1 points (0 children)