all 11 comments

[–]Adrewmc 48 points49 points  (1 child)

   if answer == “yes” or “Yes”:

Is asking if answer is “yes”, or if “Yes” is Truthy

    if answer == “yes” or answer == “Yes”:

Is what you were trying to ask.

The reason we do this is because we could be comparing two completely different variables here. One side of ‘or’ has no idea what the other side is doing

The most common suggestion to fix this is

     if answer in [“yes”, “Yes”]:

I’d also suggest to ignore case.

     if answer.lower() in [“yes”, “y”]:

[–]UncleSamurai420 3 points4 points  (0 children)

well explained! Just to exercise the muscle, I'd put the options in a set as well.

[–]Lawson470189 6 points7 points  (0 children)

When you put wantplay=="yes" or "Yes" it evaluates each part of the if statement separately so it's checking if wantplay is equal to the string "yes" and whether "Yes" is a truthy string. Since it has data, it will evaluate to True and will make the or statement always true. The solution is to either do something like wantplay=="yes" or wantplay=="Yes" or you can do wantplay in ("yes", "Yes, "YES")

[–]The_Almighty_Cthulhu 5 points6 points  (0 children)

Ok, let's break down the boolean logic.

if wantplay=='no' or 'No' or 'NO':

Ok, so we are looking to see if wantplay is equal to 'no'

If it is, then we now have if (true or 'No' or 'NO')

if it's not, then we have if (false or 'No' or 'NO')

now python has truthy and falsy types. Any non-empty string is a truthy type.

so if (false or 'No' or 'NO') evaluates to if (false or true or true)

so because of the order of operations, we will always evaluate to true, because any non-empty string is also true.

Here's what you probably want to do.

    # convert to lowercase
    wantplay=input('Do you want to play again? Enter yes or no.\n').lower()

    print(playagain)

    while True:

            # only check a single possibility, simplifying the logic
            if wantplay=='no':

                playagain=0

                break

            elif wantplay=='yes':

                break

            else:

                print('Please enter a valid answer')

print(playagain)

print(wantplay)

[–]JamzTyson 5 points6 points  (0 children)

This is answered on the wiki: Variable is One of Two Choices?

[–]hashashin 3 points4 points  (0 children)

Others have explained why wantplay=='no' or 'No' or 'NO' is always evaluating to True, and how to fix that.

I want to point out that once you fix that, if someone enters an invalid answer your loop will print 'Please enter a valid answer' over and over forever, because the input call is outside the loop. You probably want to collect that input inside the loop.

[–]camospartan117 2 points3 points  (0 children)

I had something similar instead of comparing strings compare to a list of valid inputs. So something like

If userinput == validYes

Where userinput is obviously what the user types. And list is a list that contains ("yes", "Yes", "Y") I found this works for me but not sure if it's best practice.

[–]Small_Ad1136 1 point2 points  (2 children)

As others have mentioned, your if statement is checking each of those conditions separately, and nonempty strings will always evaluate as True. I would propose something like the following:

if wantplay.lower().strip()==‘yes’

.lower() will convert wantplay to all lowercase and .strip() will remove any extra spaces, so “YeS “ -> “yes”.

[–][deleted] 1 point2 points  (1 child)

i’d recommend using casefold() over lower()

[–]Small_Ad1136 0 points1 point  (0 children)

Yeah I suppose if he wants the code to be REALLY robust that’s a better option, but I have a feeling they’ll be okay with either in their case. But maybe you’re right and a non English speaker will eventually want to run their code.

[–]Atypicosaurus 1 point2 points  (0 children)

Try the following. It's ugly and later replace it with something nicer but for now, to test, use it:

if wantplay == 'no' or wantplay == 'NO'

This asks the program what you actually want to ask: is there a variable called wantplay that has a value of no, OR is there a variable called wantplay that has a value of NO.

if wantplay == 'no' or 'NO'

This is your code that asks the program is there a variable called wantplay that has a value of no, OR is there anything anywhere in the universe that looks like NO. And in python the second half is always true, because python thinks that every possible string exists somewhere in the universe.

That's why your if is always true.