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

Still stuck

All I'm getting is "Bummer! Try again!"

My code works, but it isn't meeting the challenge.

I created the classmethod. Each item in the list is an instance of the class dice.D20. The total property returns the sum of all the instances.

I'm not sure what else to do.

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__(20)
hands.py
import dice

class Hand(list):
    def __init__(self, size=0, die_class=None, *args, **kwargs):
        if not die_class:
            raise ValueError("You must provide a die class")
        super().__init__()

        for _ in range(size):
            self.append(die_class())
        self.sort()

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

    @classmethod
    def roll(cls, size):
        return cls(size, die_class=dice.D20)

2 Answers

Steven Parker
Steven Parker
231,275 Points

You might be doing a bit too much.

The instructions don't say anything about sorting the contents of a "Hand". They also don't say anything about requiring a die_class argument to instantiate a "Hand". Or accepting args and kwargs.

Trim things down to just what the instructions required and I'll bet you pass.

Nope. Trimmed it down. Rechecked the indentations. Still works, but not accepted.

class Hand(list):

    def __init__(self, size=0, die_class=None):
        super().__init__()
        for _ in range(size):
            self.append(die_class())

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

    @classmethod
    def roll(cls, size):
        return cls(size, die_class=dice.D20)
Steven Parker
Steven Parker
231,275 Points

But you forgot to "import dice". Otherwise, I think you have it now. :+1:

Thank you so much. I was beginning to worry if I could ever make it work. It would be so much more helpful with a display of the interpreter results.