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 Python Testing Be Assertive Membership and Other Assertions

self.hand1.results[0].value

Hi there I understand the line self.hand1.results[0] but don't understand the .value part at the end of it, isn't the 'value' attribute part of the class Die and not the class Roll? I'm a bit confused to what exactly going on with it. Could someone help me thanks.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

Let's parse some code.

# a hand is the results of a dice.Roll()
self.hand3 = dice.Roll('3d6')

# the Roll __init__ function defines 'results' as:
for _ in range(num):
                self.results.append(Die(sides))

# So 'results' is an array where each item is one Die object

# In the Die object, 'value' is initialize to the random output of Die.roll() method
class Die:
    value = None

    def __init__(self, sides=6):
        try:
            assert sides >= 2
        except AssertionError:
            raise ValueError("Die needs at least 2 sides")
        else:
            self.sides = sides
        self.value = self.roll()  # define `value` based on roll() method

    def roll(self):
        return random.randint(1, self.sides)

So self.hand1.results[0].value is the value of the Die object in the first index of the results array.

Post back if you need more details.

I understand it now, thanks for the help Chris. I really appreciate it.

Flore W
Flore W
4,731 Points

Hi Chris Freeman

What's the value added of adding 'value' to 'self.hand1.results[0].value?

If I have in my console:

>>> from dice import Roll
>>> hand1 = Roll('2d6')
>>> hand1.results
[4,5]
>>> hand1.results[0]
4
>>> hand1.results[0].value
4

Can I just get away with self.hand1.results[0]? without the '.value'?