Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Dice Roller Project Breakdown

Suraj Shah
Suraj Shah
1,055 Points

TypeError: __init__

Hi,

I am attempting to replicate Kenneth's code in PyCharm IDE

code as follows:

import random

class Die:
    def __init__(self, sides = 2, value = 0):
        if not sides >= 2:
            raise ValueError("Must have a dice with 2 or more sides")
        if not isinstance(sides, int):
            raise TypeError("Number of sides must be an integer")
        self.value = value or random.randint(1, sides)
        return self.value

d6 = Die(sides = 6)
d6.value

However, I receive the following error:

Traceback (most recent call last):
  File "/Users/suraj/PycharmProjects/yatzy/dice.py", line 14, in <module>
    d6 = Die(sides = 6)
TypeError: __init__() should return None, not 'int'

I tried to browse the internet and there was a suggestion of using __int__. However, Kenneth does not use this in his example. Nevertheless, when I tried this approach, the error is eliminated but the code constantly returns 0

Please could you help me understand what is going on?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The error message says it all: __init__() should return None, not 'int'

:point_right: remove the return statement from the __init__ method.