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

Jinman Kim
Jinman Kim
5,586 Points

Need help in finding the diference

I have attached two pieces of code, one from myself and the other from someone else on the forum. I wasn't able to pass the task with my code but with the other code. The only difference I find is using different types of loop. Can anyone explain to me what I am doing wrong?

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.

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)

#my code
    @classmethod
    def roll(cls, num_roll):
        result = []
        for _ in range(num_roll):
            new_roll = D20()
            result.append(new_roll)
        return cls(result)

#borrowed code
    @classmethod
    def roll(cls, no_of_dice):
        dice_list = []
        count = 1
        while count <= no_of_dice:
            new_die = D20()
            dice_list.append(new_die)
            count +=1
        return cls(dice_list)

1 Answer

Steven Parker
Steven Parker
229,732 Points

Your code appears to be a correct solution. So I tested it by pasting it directly into the challenge and it passed.

Perhaps there was a typo in your submission that doesn't appear here?

Jinman Kim
Jinman Kim
5,586 Points

Wow that is so strange as I have tried it several times. Thanks for your help.