Python if / else推迟到错误的分支 [英] Python if/else defers to wrong branch
问题描述
我正在为学校项目编写一个互动小说游戏,出于某种原因,当我尝试将if / else语句与input(或raw_input)一起使用时,if else语句会推迟到错误的分支,而不管是什么我输入。以下是有问题的代码:
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 the wrong branch regardless of what I input. Here is the code in question:
print( """
You enter the room to the south.
Upon entering you mark that it is pitch black,
and if you were in the famous text adventure Zork,
you would be likely to be eaten by a grue. Thank
the programmer for small favors. Unlike in that game,
you have a candle, a single stick of 100-year-old
dynamite, and that matchbook from earlier.
You just knew that would be useful! The candle and
the dynamite have the same shape, size, and weight.""")
choice1 = True
while choice1 == True:
choice2 = input("Will you strike a match? ")
if choice2 == "Yes" or "yes" or "y":
print("""
It flickers for a second. You can kind of make
out which is which, but alas! They are both
covered in red paper! You are beginning to sweat
from the nervousness.""")
choice1 = False
elif choice2 == "No" or "no" or "n":
print("""
Okay. I can wait, you aren’t leaving this room until
you light a match. You will eventually light a match,
but I can wait until you do.""")
choice1 = True
else: choice1 = True
if / else语句正在处理我键入的任何内容,就好像我输入了是。有人可以帮我解决这个错误吗?
The if/else statement is treating anything that I type as though I typed yes. Can anyone help me with this error?
推荐答案
我认为你的问题出现在 if
语句的条件中,例如如果choice2 ==是或是或y
。这看起来会检查 choice2
是否是
或 choice2
是是
或 choice2
是y
,但事实并非如此。问题是或
语句。代码中的 if
语句可写为 if(choice2 ==Yes)或(yes)或( y)
并具有相同的含义。这使得更容易看到,即使 choice2
不等于是
,表达式将为true,因为string yes
非空,因此在中转换为
声明。这是因为python中的 True
或
运算符是一个布尔OR,所以如果运算符的任何一边(转换为布尔值)为true,则表达式为。解决此问题的最简单(即最少代码重构)方法是一系列 ==
:
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:
如果choice2 ==是或choice2 ==是或choice2 ==y:#...
还有其他人,但这应该是一个简单的伎俩,就像你的程序一样。如果您需要进行越来越复杂的匹配,则应该查看字符串运算符。例如,如果是.startswith(choice2.lower()):#... ,您的表达式可以重写为,但如果不理解它,请不要使用它。对于你的大小的程序,链式
==
将会很好。希望这有帮助!
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!
这篇关于Python if / else推迟到错误的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!