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

My hand isn't working. It keeps saying "Cannot get the length of a 'Hand'" IDLE returns an empty list

I can't find the problem.

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):
    @classmethod
    def roll(self):
        d1 = D20()
        d2 = D20()
        super().append(d1.value)
        super().append(d2.value)

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

2 Answers

Steven Parker
Steven Parker
229,732 Points

Here's a few hints:

  • a classmethod would have an extra first argument that represents the class itself
  • I don't think you can call append() except onto an instance
  • the roll method should take an argument for the number of D20's to roll
  • your roll method doesn't return anything yet
  • remember to return a new instance of the Hand object
Mickey Asavanant
Mickey Asavanant
8,237 Points

I stuck on this one too. I don't think I understand the question clearly. Are we supposed to create a class method or instance method? If we are going to create a class method, is the question asking to return a list of 2 D20 objects?

Steven Parker
Steven Parker
229,732 Points

The instructions gave an example of calling it as "Hand.roll(2)", which would indicate that it is a class method.

The instructions also say it should return an instance of the Hand object (with the D20 objects inside it).