程序识别我给它的每个输入都是正确的



我目前正在主持一个在线寻宝游戏,其中一个步骤是"展开"。从一捆捆的干草中寻找线索。由于某种原因,无论何时测试,任何数字都被认为是正确的。

import random
import time
bale = int(input("Say a number 1-250 to unroll a bale of hay! It will take 45 seconds before you're told if clues are inside of it, so be willing to wait.")
if int(bale) == 63 or 190 or 36 or 106 or 127 or 89 or 84 or 44 or 80 or 124 or 173 or 202 or 224 or 51 or 220:
print "Wait 45 seconds to unroll the bale."
time.sleep(15)
print "30 seconds left..."
time.sleep(5)
print "..."
time.sleep(5)
print "20 seconds left..."
time.sleep(10)
print "10 seconds..."
time.sleep(5)
print "5 seconds..."
time.sleep(1)
print "4!"
time.sleep(1)
print "3!"
time.sleep(1)
print "2!"
time.sleep(1)
print "1!"
time.sleep(1)
print "Yes! There's a clue in here! Send a screenshot of this for confirmation to move on!"
else:
print "Wait 45 seconds to unroll the bale."
time.sleep(15)
print "30 seconds left..."
time.sleep(5)
print "..."
time.sleep(5)
print "20 seconds left..."
time.sleep(10)
print "10 seconds..."
time.sleep(5)
print "5 seconds..."
time.sleep(1)
print "4!"
time.sleep(1)
print "3!"
time.sleep(1)
print "2!"
time.sleep(1)
print "1!"
time.sleep(1)
print "Sorry, no clue. Refresh the page and try again with a new number."

当您编写or子句时,每个参数都被计算为布尔值。所以实际上你写的东西被解释为:

int(bale) == 63是真的吗?或者190是真的?或者36是真的?等。

Python将这些整数转换为bool值并计算它们是否为真。注意bool(n)对除n = 0以外的所有整数n都为假,因此这导致if中的条件总是为真。

你要做的是检查int(bale)是否等于任何整数。这可以通过将检查更改为:

来完成。
if int(bale) == 63 or int(bale) == 190 or int(bale) == 36 or...

一种更易读和快速的方法是使用set:

if int(bale) in {63, 190, 36, ...}:

相关内容

  • 没有找到相关文章

最新更新