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

Andrei Oprescu
Andrei Oprescu
9,547 Points

I don't know what I did wrong

So I have a challenge that asks me to do this:

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.

I have done everything that the challenge asked me but I still get an error saying 'Can't get the length of a 'Hand''

My code is at the bottom of the question.

Can someone help me figure it out?

Thanks!

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, die_class=D20):
        super().__init__()

    def roll(self, size):
        for _ in range(size):
            self.append(die_class())

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

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

You are almost there. The following needs to be corrected:

  • Since the access is from class Hand and not an instance of Hand, the method should be decorated with @classmethod. Also, as a classmethod, use cls instead of self in the parameter list to make it clearer.
  • As a classmethod there is no instance to operation on. To create one, use hand = cls()
  • As a classmethod, die_class will not have been passed in or defined. Instead, used D20 directly. Also, remember to append to hand instead of self
  • Lastly, remember to return the object hand

Post back if you need more help. Good luck!!

I also have this error! It keeps saying

Bummer! Can't get the length of a `Hand`

My dice.py is identical to Andrei's. My hands.py is below, it works just fine in the workspace.

import dice

class Hand(list):
    def __init__(self, count=0, diecls=None, *args, **kwargs):
        if not diecls:
            raise ValueError("You must provide a die class")
        if not issubclass(diecls, dice.Die):
            raise TypeError("You must provide a type of die as die class")

        super().__init__()

        for _ in range(count):
            self.append(diecls())
        self.sort()

    @classmethod
    def roll(cls, count):
        return cls(count=count, diecls=dice.D20)

    @property
    def total(self):
        return sum(self) 
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Comment out the line self.sort. The D20 & Die class does not defined the comparison magic methods needed for sort. Actual error:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "hand.py", line 18, in roll
    return cls(count=count, diecls=dice.D20)
  File "hand.py", line 14, in __init__
    self.sort()
TypeError: unorderable types: D20() < D20()

Great! thanks for this tip. My workspace version had the gt and lt magic methods implemented, so that's why it worked there and not in the challenge.

I guess this challenge was meant to work with minimal changes, I found a shorter solution after reading discussions on other questions for this challenge - you can get away with not even overriding Hand class's init!