Typeerror:object .__ new __()不带参数(帮助)

use*_*002 4 python typeerror

我只是想制作一个生成骰子的代码(在python中).这是代码:

import random

class Dice:
     def _init_(self, number_dice):
          self._dice = [6] * number_dice

     def roll_dice(self):
          for d in range(len(self._dice)):
               self._dice[d] = random.randit(1, 6)
          self._dice.sort()

     def print_roll(self):
          length = len(self._dice)
          print(str(lenth) + "dice:" + str(self._dice))



my_dice = Dice(2)
my_dice.roll_dice()
my_dice.print_roll()
Run Code Online (Sandbox Code Playgroud)

编译器对第18行说了些什么.我是编程的新手,所以任何事情都有帮助=]

iCo*_*dez 11

你需要前后两个下划线__init__:

def __init__(self, number_dice):
    self._dice= [6] *number_dice
Run Code Online (Sandbox Code Playgroud)

否则,Python将该方法视为自定义方法而不是特殊__init__构造方法.

  • 有趣的事实:双下划线模式被称为"dunder". (4认同)

scr*_*ter 5

此时应更换_init___init__


归档时间:

查看次数:

8645 次

最近记录:

12 年 前