Typeerror: object.__new__() takes no parameters (help)

匿名 (未验证) 提交于 2019-12-03 02:26:02

问题:

I'm simply trying to make a code that generates dice (in python). Here's the code:

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() 

The compiler says something about line 18. I'm new to programming so anything helps =]

回答1:

You need two underscores before and after __init__:

def __init__(self, number_dice):     self._dice= [6] *number_dice 

Otherwise, Python treats that method as a custom one and not the special __init__ constructor method.



回答2:

You should replace _init_ with __init__



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!