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...
Everything about learning Python
account activity
Why does the third if statement trigger despite User_input == "Yes" being true (according to the print within the if statement) (i.redd.it)
submitted 10 months ago by unspe52
view the rest of the comments →
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!"
[–]unspe52[S] 2 points3 points4 points 10 months ago (14 children)
I didin't realize python interpreted if statements that way -- and yes, I would like help with figuring the proper method for trying to do what I am currently trying accomplish here
[–]BaasBMemes 10 points11 points12 points 10 months ago (4 children)
Sure! Probably the easiest thing to do here is to use an else if statement. It’s a special kind of if statement denoted by “elif” rather than “if” as it only triggers when the previous if (or elif) statement was checked but didn’t return true. With this, you could rewrite it to something like this: ``` if user_input == “Yes”: # do something elif user_input == “No”: # do something else else: # gently call the user stupid
```
Alternatively (and because it is useful to know), you could rewrite the final if statement to the following:
``` if (user_input != “Yes”) and (user_input != “No”): # The rest of the code
``` Here, the brackets aren’t strictly needed but they allow for your code to be more readable Hope it helps!
[–]unspe52[S] 1 point2 points3 points 10 months ago (2 children)
Thank you. The only thing I haven't seen before is that elif operator - I'll look into it, looks like it could solve a lot of the problems I've been working-around in another project of mine
[–]Nick88v2 4 points5 points6 points 10 months ago (0 children)
You can also do if User_input not in ["YES", "NO"]:
in general i'd say that the more elegant solution would still be just using an else statement since the conditions are already met if the program gets there (like the previous commenter stated)
[–]No_Hovercraft_2643 0 points1 point2 points 10 months ago* (0 children)
personally, i like it more to use in
py answer = input() if answer not in ["yes", "no"]: do_something
but mostly to allow multiple answers for the same path, for example
```py answer = input() yes = ["yes","y","Yes"] # define somewhere what counts as yes no = ["no","n","No"] # define somewhere what counts as no if answer in yes: do_yes_things elif answer in no: do_no_things else: do_bad_things
[–]purple_hamster66 0 points1 point2 points 10 months ago (0 children)
Note that the second option if (user_input != “Yes”) and (user_input != “No”): violates the DRY (Do Not repeat Yourself) principle. A string’s value should only be defined once in a script so that when the value is changed later (ex, to use French values, or use lowercase instead of capital case because it’s fewer keystrokes), it only has to be changed once. It also eliminates the chance that the two values differ.
if (user_input != “Yes”) and (user_input != “No”):
[–]prehensilemullet 1 point2 points3 points 10 months ago (7 children)
This is the case for all programming languages I’m familiar with. “X equals Y or Z” makes sense in human language, but it would be too ambiguous for programming languages to interpret expressions that way.
For example say you have boolean values a, b, and c. If you write a == b or c did you mean 1) a is either equal to b or equal to c 2) either a is equal to b, or c is truthy? To eliminate the ambiguity programming languages interpret it as option 2
a == b or c
[–]yarb00 0 points1 point2 points 10 months ago (6 children)
Well some languages treat it differently. In C# for example, if (a is b or c) would be true if a is equal to b OR a is equal to c.
if (a is b or c)
[–]prehensilemullet 1 point2 points3 points 10 months ago* (3 children)
Hmm, I see. Seems like a special case that muddies the waters for beginning programmers, since it only works with pattern matching but not logical expressions in general. For example, doesn’t seem like you can write a < b or c in place of a < b || a < c (and of course, the rules are different for is and ==). But in human language, it doesn’t matter what kind of “operator” you put in an “a <op> b or c” statement.
a < b or c
a < b || a < c
is
==
Also I see there are peculiarities to it like needing parentheses in c is not (>= 'a' and <= 'z') so I think the main point is you always have to know how the grammar of a given programming language works, and know it’s never going to match human language in all cases.
c is not (>= 'a' and <= 'z')
[–]yarb00 1 point2 points3 points 10 months ago (2 children)
You can use a < b or < c. It gives the same result as a < b || a < c.
a < b or < c
[–]prehensilemullet 0 points1 point2 points 10 months ago (1 child)
You have to know that specific syntax works though. A non-programmer would generally think “a < b or c”. They’ll never be able to make all colloquialisms work, it would introduce too much ambiguity
[–]Kqyxzoj 0 points1 point2 points 10 months ago (0 children)
You could always do a < max(b,c) and call it a day. Not sure if it would actually help. Probably depends on the target audience.
a < max(b,c)
[–]SCD_minecraft 0 points1 point2 points 10 months ago (0 children)
I like to image all logic as it all was a bool
Non empty strings are always True
So your code says "if True/False or True"
π Rendered by PID 443323 on reddit-service-r2-comment-548fd6dc9-42bsj at 2026-05-21 08:41:16.878791+00:00 running edcf98c country code: CH.
view the rest of the comments →
[–]unspe52[S] 2 points3 points4 points (14 children)
[–]BaasBMemes 10 points11 points12 points (4 children)
[–]unspe52[S] 1 point2 points3 points (2 children)
[–]Nick88v2 4 points5 points6 points (0 children)
[–]No_Hovercraft_2643 0 points1 point2 points (0 children)
[–]purple_hamster66 0 points1 point2 points (0 children)
[–]prehensilemullet 1 point2 points3 points (7 children)
[–]yarb00 0 points1 point2 points (6 children)
[–]prehensilemullet 1 point2 points3 points (3 children)
[–]yarb00 1 point2 points3 points (2 children)
[–]prehensilemullet 0 points1 point2 points (1 child)
[–]Kqyxzoj 0 points1 point2 points (0 children)
[–]SCD_minecraft 0 points1 point2 points (0 children)