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 trialcathal britton
16,664 Pointsself.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
Treehouse Moderator 68,457 PointsLet'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.
cathal britton
16,664 Pointscathal britton
16,664 PointsI understand it now, thanks for the help Chris. I really appreciate it.
Flore W
4,744 PointsFlore W
4,744 PointsHi Chris Freeman
What's the value added of adding 'value' to 'self.hand1.results[0].value?
If I have in my console:
Can I just get away with self.hand1.results[0]? without the '.value'?