Python 巨蟒骰子游戏
我必须创建一个骰子游戏,生成从1到6的数字。然后掷骰子50次,计算奇数和偶数。我正在使用Python 这是我的密码:Python 巨蟒骰子游戏,python,Python,我必须创建一个骰子游戏,生成从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 =
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.")
您的循环错误,应该是:
while throws != maxthrows:
throws += 1
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
请注意:
- 只要可能,退出条件应在循环条件中使用,而不是在
if。。。中断
- 在Python中,询问骰子是否奇数的部分必须在循环内部,缩进很重要-很多李>
原始输入是用来做什么的?您还应该将所有代码放在一个函数中,并在一个if\uuuuu name\uuuu==“\uuuu main\uuuu”
guard之后调用该函数。顺便问一下,你的问题是什么?