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

Ricardo Franco
seal-mask
.a{fill-rule:evenodd;}techdegree
Ricardo Franco
Data Analysis Techdegree Student 16,258 Points

RPG Roller Code Challenge

My error is reading "Got the wrong length for a 'Hand()'. I am clearly getting some kind of length response with my current code which is good but I can't seem to get past that hump. Thank you, in advance, for your time and assistance.

dice.py
import random


class Die:
    def __init__(self, sides=2, value=0):
        if sides < 2:
            raise ValueError("Can't have fewer than two sides")
        if not isinstance(sides, int):
            raise ValueError("Sides must be a whole number")
        if not isinstance(value, int):
            raise ValueError("Value must be a whole number")
        self.sides = sides
        self.value = value or 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=None):
        self.size = size

    @classmethod
    def roll(cls, dice_roll):
        size = []
        for _ in range(dice_roll):
            size.append(D20())   
        return cls(size)

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

#    def __len__(self):
#        return len(self)
Aaron Jorgensen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Aaron Jorgensen
Python Development Techdegree Graduate 13,658 Points

I tried your code and it passed. Not sure why you would be getting that message. Try restarting the challenge. If it persists, maybe post the error message here.

1 Answer

Steven Parker
Steven Parker
231,269 Points

The "roll" method itself looks good. :+1:   But two issues stand out:

  • you don't need to override "__init__"
  • don't modify the provided "total" method code