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 Giving a Hand

Amir Shahabnia
Amir Shahabnia
29,310 Points

Get AttributeError while trying to get hand[0].value

I am trying to get the value from an instance of the Hand class but I get an

AttributeError when I try hand[0].value assigned with die_class=D6. 

Everything seems to be working but not the value of each of dices. Please help :))

Here is my code for the class:

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


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

Below is my code implemented on terminal including the Attribute Error:

>>> from dice import D6
>>> from hands import Hand
>>> hand = Hand(size=5, die_class=D6)
>>> hand[0]
<class 'dice.D6'>
>>> hand[0].value
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'D6' has no attribute 'value'

1 Answer

Nehemie Balukidi
Nehemie Balukidi
405 Points

Hi there, the error says that a D6 object does not have an attribute named value. Can you try to do something like: dir(D6) or dir(hand[0]) to see the attributes of a D6 object, post the output back if you still can't find a way forward? so we can try to help