Skip to content Skip to sidebar Skip to footer

Python If/else Defers To Wrong Branch

I'm writing an Interactive Fiction game for a school project, and for some reason, when I try to use an if/else statement with input (or raw_input), the if else statement defers to

Solution 1:

Your if statements are wrong, they should look like this:

if choice in ["Yes", "yes", "y"]:
    …

or like this:

if choice == "Yes"or choice == "yes"or choice == "y":
    …

Python treats an non-empty string as true, e.g. "Yes" is considered true. So if you write choice == "yes" or "Yes", the expression is always true because if even if choice == "yes" isn't true, "Yes" will be considered true.

Solution 2:

I believe your problem is in the if statements' conditionals, such as if choice2 == "Yes" or "yes" or "y". This looks like it would check whether choice2 is "Yes" or choice2 is "yes" or choice2 is "y", but it doesn't. The problem is the or statement. The if statement in your code could be written as if (choice2 == "Yes") or ("yes") or ("y") and have the same meaning. This makes it easier to see that, even if choice2 does not equal Yes, the expression will be true because the string "yes" is non-empty and as thus is converted to True in an if statement. This is because the or operator in python is a boolean OR, so if either side of the operator, converted to a boolean, is true, then the expression is. The easiest (i.e. least code refactoring) way to fix this is a series of =='s:

if choice2 == "Yes" or choice2 == "yes" or choice2 == "y": #...

There are others, but this should do the trick for a simple-is program like yours. If you need to do more and more complex matching, you should look into string operators. FOr example, your expression could be rewritten as if "yes".startswith(choice2.lower()): #..., but don't use this without understanding it. For a program of the size of yours, the chained =='s will do just fine. Hope this helps!

Solution 3:

The line if choice2 == "Yes" or "yes" or "y" doesn't work like you think it does. After the first statement choice2 == "Yes" it is like asking if "yes" or if "y". The if statement on a string will always return true, unless it is an empty string. To fix this you'll need either

ifchoice2== "Yes"orchoice2== "yes"orchoice2== "y":

or this more pythonic approach:

if choice2 in ["Yes", "yes", "y"]:

which will check if the string is in that array. Of course the same thing applies to elif choice2 == "No" or "no" or "n":, which in its current form will also always return true.

Post a Comment for "Python If/else Defers To Wrong Branch"