Python if 语句未按预期工作

发布于 2024-12-08 01:51:12 字数 277 浏览 0 评论 0原文

我目前的代码是:

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 不断打印为 3 或 4,但我继续得到结果“你逃跑失败!” ,谁能告诉我为什么会这样?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

葬花如无物 2024-12-15 01:51:12

表达式fleechance == 1 or 2 等价于(fleechance == 1) or (2)。数字 2 始终被视为“true”。

试试这个:

if fleechance in (1, 2):

编辑:根据你的情况(只有两种可能性),以下内容会更好:

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

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

Try this:

if fleechance in (1, 2):

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

if fleechance <= 2:
    print "You failed to run away!"
else:
    print "You got away safely!"
懒猫 2024-12-15 01:51:12

尝试

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

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

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

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!"
段念尘 2024-12-15 01:51:12

if 语句按设计工作,问题是操作顺序导致此代码执行与您想要的不同的操作。

最简单的解决方法是:

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

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.

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!"
情深已缘浅 2024-12-15 01:51:12

因为您不是在问 fleechance 是 1 还是 fleechance 是 2;您询问的是

  1. fleechance 是否为 1,或者
  2. 2 是否非零。

当然,条件的第二部分始终为真。尝试

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

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

  1. fleechance is 1, or
  2. 2 is non-zero.

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

if fleechance == 1 or fleechance == 2:
    ...
壹場煙雨 2024-12-15 01:51:12

您编写 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!"

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 :

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!"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文