【发布时间】:2016-05-24 22:07:18
【问题描述】:
这个函数 (playCraps()) 应该选择 1 到 6 之间的两个随机整数,将它们相加,然后返回 x 中保存的 True 或 False 值。
出于某种原因,每次我运行程序时,它总是返回 True 和 7 或 11。
import random
wins = [7, 11]
losses = [2, 3, 12]
def playCraps():
x = 0
while x == 0:
sumDice = random.randint(1,6) + random.randrange(1,6)
if sumDice in wins:
x = True
elif sumDice in losses:
x = False
else:
sumDice = 0
print("The sum of the dice was " + str(sumDice))
print("So the boolean value is " + str(x))
为什么会这样?如何解决这个问题?
【问题讨论】:
-
这不是有效的 python 代码。能否请您修复缩进,以便我们追踪错误?
-
缩进是否正确?
标签: python if-statement random while-loop