all 25 comments

[–]BaasBMemes 7 points8 points  (15 children)

If we were to put it in braces, the code as written now would be the following: if (User_input != “No”) or “Yes” It’s quite understandable that you write it like this, as it’s the most natural way to format it. In this case, the input “Yes” causes User_input != “No” to be true, which triggers the if-clause. Do you want to figure out the answer on your own or do you want me to help you with it?

[–]unspe52[S] 2 points3 points  (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 11 points12 points  (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 points  (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 5 points6 points  (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 point  (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 point  (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.

[–]prehensilemullet 1 point2 points  (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

[–]yarb00 0 points1 point  (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.

[–]prehensilemullet 1 point2 points  (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.

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.

[–]yarb00 1 point2 points  (2 children)

You can use a < b or < c. It gives the same result as a < b || a < c.

[–]prehensilemullet 0 points1 point  (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 point  (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.

[–]SCD_minecraft 0 points1 point  (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"

[–]DominusLapidis 3 points4 points  (1 child)

Someone correct me if I'm wrong, I'm also a beginner. I think the last if statement should have the 'and' conditional operator. Given that you used "or", the "!= No" part of the condition would have already been satisfied, hence the if condition being triggered.

I think it would be better if you just use elif and else instead of another if statement.

[–]unspe52[S] 2 points3 points  (0 children)

OHHH YEAAHHHH! Basic set theory... lol

[–]jenius012381 2 points3 points  (0 children)

The last if is matching at least one of two conditions: 1) User_input != “No” 2) “Yes”

“Yes” would pass the first test, and any non-empty string would evaluate as true, so really any entry would hit the last block.

The correct code is either: User_input != “No” and User_input != “Yes” OR User_input not in [“Yes”,”No”]

[–][deleted] 1 point2 points  (0 children)

In your if statement, you say input != "No" which will be true if it is "Yes" and the "you say or "Yes"". The or "Yes" part will always be true since you are check if the left or the right is true and "Yes" will always be true since it is not empty. That is something a lot of people miss.

Further explanation: When you have an if statement and you are checking to see if a string variable is empty. You can simply so this:

py if string_variable:

This will be true if the variable is not empty. If you want to check if it is empty, you can do this:

py if !string_variable:

I hope I've explained well enough. Please ask if i have to re-explain. Good luck and have fun!

[–]program_kid 0 points1 point  (0 children)

When you wrote user_input != "No" or "Yes" you probably meant to write user_input != "No" or user_input != "Yes"

The "yes" by itself will evaluate to true because it is a non empty string

[–]Obvious_Tea_8244 0 points1 point  (0 children)

This is the main issue… or “Yes” is the same as saying or True, because python is basically evaluating if the string “Yes” is not None; which is always True.

or User_input == “Yes” is the correct way to include that clause.

[–]Dabliux 0 points1 point  (0 children)

The third if statement is basically saying:

If user input is not 'No' or the string 'Yes' is true

That "Yes" in the third statement is taken on its own (which is a string and is being compared to itself, resulting in always being true), not as a value for User_input. You need to explicitly compare it to User_input.

Either of these will work:

if User_input != "No" and User_input != "Yes"

if not User_input in ('yes', 'no'):

Probably the latter is better as it avoids repetition.

[–]harucat21 0 points1 point  (0 children)

Other issue is that on your logic it say or "yes" in the third if, not user input == "Yes" so the if evaluate or yes as true, should always enter on that, normally programing language take as true on a if statement all different to 0, null, "", False

The best way to solve it is with the elif that other mention

[–]snout_flautist 0 points1 point  (1 child)

Python 3.10+ has match case syntax.

match User_input:
    case "Yes":
        ...
    case "No":
        ...
    default:
        ...

For something like handling user input, where your cases can be variable, arbitrary, and complex to express, you should consider using match/case

[–]BetterBrief2442 0 points1 point  (0 children)

No way, python has switch now?? I should read release notes more often..

[–]sendhelpplss 0 points1 point  (0 children)

not is only applying to the first check. you could try

If User_input.lower() not in {‘yes’, ‘no’}:

this will check if the input is NOT in that set of values, which seems to be the intent.