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 RPG Roller

Pitrov Secondary
Pitrov Secondary
5,121 Points

I am lost on task 2

I have not Idea of what is wrong. Anyone help?

dice.py
import random


class Die:
    def __init__(self, sides=2):
        if sides < 2:
            raise ValueError("Can't have fewer than two sides")
        self.sides = sides
        self.value = random.randint(1, sides)

    def __int__(self):
        return self.value

    def __add__(self, other):
        return int(self) + other

    def __radd__(self, other):
        return self + other

class D20(Die):
    def __init__(self):
        super().__init__(sides=20)
hands.py
from dice import D20
class Hand(list):
    def __init__(self, size=2, dice_type=None):
        if not dice_type:
            raise TypeError

        for _ in range(size):
            self.append(dice_type())

    @classmethod
    def roll(cls, num=2, dice_type=D20):
        hand_round = cls()
        for _ in range(num):
            hand_round.append(dice_type())

        return hand_round

    @property
    def total(self):
        return sum(self)

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Pitrov,

In your Hand constructor you specify:

def __init__(self, size=2, dice_type=None):
        if not dice_type:
            raise TypeError

Then in your roll class method you do this:

 hand_round = cls()

Which attempts to instantiate an instance of Hand with the default arguments (size=2 and dice_type=None).

The first line of the constructor checks whether dice_type exists (i.e., is not None) and then raises.

If you fix that by passing your class method call dice_type as an argument, you will no longer get a crash but you will hit the other problem: you create two more dice than the number you intend.

Hope that points you in the right direction.

Cheers,

Alex