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

Devin Dubin
Devin Dubin
4,426 Points

Creating Hand with 2 D20 Rolls

I keep getting an error for incorrect length of Hand

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,sides=20):
        super().__init__()
        self.sides = sides
hands.py
from dice import D20

class Hand(list):

    def __init__(self,size):
        super().__init__()



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

    @classmethod
    def roll(cls,die_num):
        output = []
        for i in range(die_num):
            output.append(D20())
        return cls(output)

2 Answers

Steven Parker
Steven Parker
231,248 Points

You almost have it. You just don't need to override "__init__", and doing so caused it to require an argument that the challenge isn't prepared to use.

Just remove the "__init__" override and you'll pass the challenge.

Julian Addison
Julian Addison
13,302 Points

I'd recommend creating the class instance first and appending directly to it within the for loop. The class Hand behaves like a list already, so it will inherit list methods like append(). You don't need to declare the empty list variable output, and you don't need to pass any arguments when declaring your new class instance. Also, as Steven said, there is no need to override the init method within the Hand class. Just stick the the roll method as the instructions say. You are definitely close