我在这里只是源代码的一小部分,我是一个新手,本月才开始学习并尝试使此游戏正常工作,我不知道这里有多少工作。我的问题是,当n1或n2与我输入的数字相同时,我仍然要在第一个“ if”中循环运行,我希望它在最后一个“ elif”中运行。我现在遇到的代码问题,因为我听不懂...如果我的英语不好,请不要介意我...并且仍然可以使我的成绩有效,因为经过一周的尝试,我仍然没有线索..
随机导入
def EasyMode():
n1 = random.randint(5, 9)
n2 = random.randint(1, 4)
player = int(input("Select a room from 1 to 9: "))
# while n0 != player:
while True:
if player != n1 or n2:
print("You have been survire!")
print("First Trapper", n1)
print("Second Trapper", n2)
ScoreCountE()
elif player > 10:
print("STOP CHEATING")
ScoreCountE()
elif player == n1 or n2:
print("You were capture by the trapper" + '\n' + "Game Over!")
break
else:
break
def ScoreCountE():
score = 0
while score >= 0:
score = score + 1
print(score)
return score and EasyMode()
EasyMode()
答案 0 :(得分:1)
原因很简单:elif player != n1 or n2
与elif player != n1 or player != n2
不同。
player != n1 or n2
与(player != n1) or (bool(n2))
相同。换句话说,只要n2
是“真实的”(某种东西,当转换为布尔值时将是True
),player != n1 or n2
将是True
。由于n2
始终是非零的int
,因此它始终是真实的,因此您的if
条件将始终成功。
此外,您只能在player
循环的外部之外更改n1
,n2
和while
的值。这意味着您在该循环内的代码将始终采用相同的路径!从您的代码来看,我认为您想在循环中使用player = int(input("Select a room from 1 to 9: "))
。
顺便说一句:如果您曾经想将不平等与许多变量进行比较(例如player != n1 or player != n2 or player != n3...
),则可以改用player in [n1, n2, n3...]
。