Python if语句不能按预期工作

时间:2022-01-11 21:47:22

I currently have the code:

我目前有代码:

fleechance = random.randrange(1,5)
print fleechance
if fleechance == 1 or 2:
    print "You failed to run away!"
elif fleechance == 4 or 3:
    print "You got away safely!"

fleechance is constantly printing as 3 or 4, but I continue to get the result "You failed to run away!" ,can anyone tell me why this is happening?

fleechance不断打印为3或4,但我继续得到结果“你没能逃跑!” ,任何人都可以告诉我为什么会这样吗?

5 个解决方案

#1


9  

The expression fleechance == 1 or 2 is equivalent to (fleechance == 1) or (2). The number 2 is always considered “true”.

表达式fleechance == 1或2相当于(fleechance == 1)或(2)。数字2始终被视为“真实”。

Try this:

尝试这个:

if fleechance in (1, 2):

EDIT: In your situation (only 2 possibilities), the following will be even better:

编辑:在你的情况下(只有2种可能性),以下将更好:

if fleechance <= 2:
    print "You failed to run away!"
else:
    print "You got away safely!"

#2


3  

Try

尝试

if fleechance == 1 or fleechance == 2:
    print "You failed to run away!"
elif fleechance == 4 or fleechance == 3:
    print "You got away safely!"

Alternatively, if those are the only possibilites, you can do

或者,如果这些是唯一的可能性,你可以这样做

if fleechance <= 2:
    print "You failed to run away!"
else:
    print "You got away safely!"

#3


1  

The if statement is working as designed, the problem is that order of operations is causing this code to do something other than that you want.

if语句按设计工作,问题是操作顺序导致此代码执行除您想要的操作之外的其他操作。

The easiest fix would be to say:

最简单的解决方法是:

if fleechance == 1 or fleechance == 2:
    print "You failed to run away!"
elif fleechance == 3 or fleechance == 4:
    print "You got away safely!"

#4


1  

Because you're not asking whether fleechance is 1 or fleechance is 2; you're asking whether

因为你不是在问逃生是1还是逃亡是2;你问是否

  1. fleechance is 1, or
  2. 逃亡是1,或
  3. 2 is non-zero.
  4. 2是非零的。

Of course, that second part of the condition is always true. Try

当然,条件的第二部分总是如此。尝试

if fleechance == 1 or fleechance == 2:
    ...

#5


1  

The way you wrote your if statements is wrong. You tell python to check if fleechance equals 1 is true or if 2 is true. A non-zero integer always means true in a condition. You should wrote :

你编写if语句的方式是错误的。你告诉python检查fleechance是否等于1是否为真,或者2是否为真。在条件中,非零整数始终表示为真。你应该写道:

fleechance = random.randrange(1,5)
print fleechance
if fleechance == 1 or fleechance == 2:
    print "You failed to run away!"
elif fleechance == 4 or fleechance == 3:
    print "You got away safely!"

#1


9  

The expression fleechance == 1 or 2 is equivalent to (fleechance == 1) or (2). The number 2 is always considered “true”.

表达式fleechance == 1或2相当于(fleechance == 1)或(2)。数字2始终被视为“真实”。

Try this:

尝试这个:

if fleechance in (1, 2):

EDIT: In your situation (only 2 possibilities), the following will be even better:

编辑:在你的情况下(只有2种可能性),以下将更好:

if fleechance <= 2:
    print "You failed to run away!"
else:
    print "You got away safely!"

#2


3  

Try

尝试

if fleechance == 1 or fleechance == 2:
    print "You failed to run away!"
elif fleechance == 4 or fleechance == 3:
    print "You got away safely!"

Alternatively, if those are the only possibilites, you can do

或者,如果这些是唯一的可能性,你可以这样做

if fleechance <= 2:
    print "You failed to run away!"
else:
    print "You got away safely!"

#3


1  

The if statement is working as designed, the problem is that order of operations is causing this code to do something other than that you want.

if语句按设计工作,问题是操作顺序导致此代码执行除您想要的操作之外的其他操作。

The easiest fix would be to say:

最简单的解决方法是:

if fleechance == 1 or fleechance == 2:
    print "You failed to run away!"
elif fleechance == 3 or fleechance == 4:
    print "You got away safely!"

#4


1  

Because you're not asking whether fleechance is 1 or fleechance is 2; you're asking whether

因为你不是在问逃生是1还是逃亡是2;你问是否

  1. fleechance is 1, or
  2. 逃亡是1,或
  3. 2 is non-zero.
  4. 2是非零的。

Of course, that second part of the condition is always true. Try

当然,条件的第二部分总是如此。尝试

if fleechance == 1 or fleechance == 2:
    ...

#5


1  

The way you wrote your if statements is wrong. You tell python to check if fleechance equals 1 is true or if 2 is true. A non-zero integer always means true in a condition. You should wrote :

你编写if语句的方式是错误的。你告诉python检查fleechance是否等于1是否为真,或者2是否为真。在条件中,非零整数始终表示为真。你应该写道:

fleechance = random.randrange(1,5)
print fleechance
if fleechance == 1 or fleechance == 2:
    print "You failed to run away!"
elif fleechance == 4 or fleechance == 3:
    print "You got away safely!"