我必须创建一个从1到6生成数字的骰子游戏。然后它将掷骰子50次,它将计算奇数和偶数的数量。我正在使用Python。
这是我的代码:
import random
# Determine odd and even numbers
throws = 0
even = 0
odd = 0
maxthrows = 50
print "Even : Odd"
while True:
throws += 1
if throws == maxthrows:
break
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
print even, " : ", odd
raw_input("Press enter to exit.")
答案 0 :(得分:4)
您的循环错误,应该是:
while throws != maxthrows:
throws += 1
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
请注意:
if ... break