我是一名新的python程序员,我正在创建一个程序,该程序将随机生成骰子程序,该程序将选择要使用的骰子的多少个面,因此稍后,我可以弄清楚如何打印频率。很多次骰子降落在那个数字上。我收到“ 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()函数时

def freqRolls(sides):
    for i in range(1, len(sides)) :
        print("%2d: %4d" % (i, sides[i]))


“ sides”是一个参数,它表示函数需要一个值,并且仅在函数内部将其称为“ sides”。为了使函数正常工作,您需要在调用它时立即传递该值,如下所示:

 freqRolls(numSides)

关于python - 尝试修复TypeError:freqRolls()缺少1个必需的位置参数:“sides” python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52693558/

10-12 22:20
查看更多