Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 巨蟒骰子游戏_Python - Fatal编程技术网

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 =

我必须创建一个骰子游戏,生成从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.")

您的循环错误,应该是:

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之后调用该函数。顺便问一下,你的问题是什么?