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

Henry Lin
Henry Lin
11,636 Points

My code doesn't pass but worked fine on workspace!!!

I don't know what is wrong with code. Can someone 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=0, dice_class=D20):
        self.size = size
        super().__init__()

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

    @classmethod
    def roll(cls, num_dice):
        return cls(num_dice)

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

1 Answer

Steven Parker
Steven Parker
231,236 Points

I see two issues:

  • you wrote "dice_calss" instead of "dice_class"
  • don't sort the Hand, I'm not sure why but it messes up the check
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

The sorting raises a TypeError because the class D20 does not have a defined __cmp__ method needed by the sort() method to make comparisons. Running in the REPL shows the issue:

>>> Hand.roll(2)
Traceback (most recent call last):
  File "<string>", line 41, in <module>
  File "<string>", line 35, in roll
  File "<string>", line 31, in __init__
TypeError: unorderable types: D20() < D20()
Steven Parker
Steven Parker
231,236 Points

That makes sense, but then how would it have worked in the workspace?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

I would need to see a snapshot of the workspace to know. It is not clear the exact conditions where it "worked".

Henry Lin, could post a snapshot link of the workspace where this worked?

Henry Lin
Henry Lin
11,636 Points

Chris Freeman I have figureed it out. First, I had typo in my challenge but not in workspace. Second, I used D6 class in workspace. And I deleted the sort function, and it passed the challenge. Thanks.

Steven Parker
Steven Parker
231,236 Points

Where was "D6" defined? Did it include a "__cmp__" method?