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

George Clement
George Clement
2,816 Points

this works for me in the IDE but the challenge keeps telling me it can't get length

from dice import Hand obj_hand = Hand() obj_hand.roll(4) obj_hand [17, 11, 3, 4] obj_hand.total() 35 obj_hand.len <method-wrapper '__len__' of Hand object at 0x0000023BC88BB8B8> obj_hand.len() 4 len(obj_hand) 4

not sure what I am missing to trigger the issue

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
class Hand(list):

    def clear_hand(self):
        del self[:]

    def total(self):
        return sum(self)

    def roll(self, amt_dice):
        x = 1
        while x <= amt_dice:
            foo = D20()
            self.append(int(foo))
            x = x + 1

1 Answer

Hints:

  • roll should be a @classmethod
  • roll should not modify an instance, instead it should create and instance with the random D20s and return it
  • The new hand should contain D20 instances, not ints (it seems that you are converting the D20s to ints)
George Clement
George Clement
2,816 Points

thanks for the comment I think I'll have to think about it some more.