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

Sam Hudson
Sam Hudson
1,670 Points

Now update Hand in hands.py. I'm going to use code similar to Hand.roll(2)

I'm completely lost with this one!

Do I need to override init, or maybe use a class method?

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):
    @property
    def total(self):
        return sum(self)

    @classmethod
    def roll(cls, no_of_dice):
        dice_list = []
        count = 0
        while count <= no_of_dice:
            new_die = D20()
            dice_list.append(new_die)
            count +=1
        return(cls, dice_list)
Sam Hudson
Sam Hudson
1,670 Points

Full question: Now update Hand in hands.py. I'm going to use code similar to Hand.roll(2) and I want to get back an instance of Hand with two D20s rolled in it. I should then be able to call .total on the instance to get the total of the two dice.

I'll leave the implementation of all of that up to you. I don't care how you do it, I only care that it works.

3 Answers

ZHAO LIU
ZHAO LIU
1,554 Points

Hi sam,

I just saw your post,it is really helpful to me. This might be late, but just change the "return (cls, dice_list) " to "return cls( dice_list)" works. Since the hand inherited from the list, with "cls(dice_list)" it will instantiate an new instance.

Steven Parker
Steven Parker
231,248 Points

I've seen some solutions where people wrote __init__ overrides, but it's certainly not necessary.

But your idea about using a class method is very good, since the "roll" method is expected to return a new class instance.

Sam Hudson
Sam Hudson
1,670 Points

I've tried the above but get the following message:

Bummer: Got the wrong length for a Hand

Not really sure what's going on here, and would be really grateful for some more guidance!

Steven Parker
Steven Parker
231,248 Points

We'll need to see your "hands.py" code again with your changes applied. Properly formatted of course.

"I want to get back an instance of Hand with two D20s rolled in it"

You didn't set the ’no_of_dice’ to 1 So that in the while loop, it will append twice