4

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user2962002
  • 49
  • 1
  • 2

2 Answers2

11

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.

Community
  • 1
  • 1
5

You should replace _init_ with __init__

scriptmonster
  • 2,741
  • 21
  • 29