Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Dice Roller RPG Roller

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

python: dice-roller/rpg-roller 2/2 | Error

Im having the following error:

AttributeError: 'list' object has no attribute 'total'

I google it and the problem seems to be that function catch a list an it can't be process in the @property method.

im a little stuck here :/

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, sides=20):
        super().__init__(sides)
hands.py
from dice import D20

class Hand(list):
    def __init__(self, size=0, die_class=None, *args, **kwargs):
        if not die_class:
            raise ValueError("You must define a dice class")
        super().__init__()

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


    # 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. 
    @classmethod
    def roll(cls, dice):
        dr = []
        for _ in range(dice):
            dr.append(cls(size=2, die_class=D20))

        return dr  

    # I should then be able to call .total on the instance 
    # to get the total of the two dice.
    @property
    def total(self):
        data = self

        return sum(data[0])

1 Answer

Steven Parker
Steven Parker
216,017 Points

You don't have to create an __init__ override for this challenge, but if you do you must be careful not to change the method signature.

In particular you should not make any arguments mandatory for creating a new instance, since none were needed before.

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

Thanks Steven Parker , i swa my mistake and yes i don't need to override the init .

Thanks for your advice and time !