我是一个新的python程序员,我正在创建一个程序,它将随机生成dice程序,它将选择它将要使用的骰子的边数,这样我就可以在以后计算出如何打印骰子落在这个数字上的次数的频率。我得到一个“TypeError:freqRolls()缺少1个必需的位置参数:‘sides’错误,当试图打印出骰子从1开始到程序决定使用的边的数量时。在
import random
listRolls = []
#Randomly choose the number of sides of dice between 6 and 12
#Print out 'Will be using: x sides' variable = numSides
def main() :
global numSides
global numRolls
numSides = sides()
numRolls = rolls()
rollDice()
listPrint()
freqRolls()
def rolls() :
x = (random.randint(200, 500))
print('Ran for: %s rounds' %(x))
return x
def sides():
y = (random.randint(6, 12))
print('Will be using: %s sides' %(y))
return y
def freqRolls(sides):
for i in range(1, len(sides)) :
print("%2d: %4d" % (i, sides[i]))
# Face value of die based on each roll (numRolls = number of times die is
thrown).
# numSides = number of faces)
def rollDice():
i = 0
while (i < numRolls):
x = (random.randint(1, numSides))
listRolls.append(x)
# print (x)
i = i + 1
# print ('Done')
def listPrint():
for i, item in enumerate(listRolls):
if (i+1)%13 == 0:
print(item)
else:
print(item,end=', ')
main()
当您在这段代码中声明freqrolls()函数时
“sides”是一个参数,它意味着函数需要一个值,并且只在函数内部将其称为“sides”。对于函数工作,您需要在调用它的瞬间传递该值,如下所示:
^{pr2}$相关问题 更多 >
编程相关推荐