本文介绍了类型错误:object.__new__() 不带参数(帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想编写一个生成骰子的代码(在 python 中).代码如下:

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

编译器说了一些关于第 18 行的内容.我是编程新手,所以任何事情都有帮助 =]

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

推荐答案

__init__ 前后需要两个下划线:

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

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

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

这篇关于类型错误:object.__new__() 不带参数(帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:33