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
Stefano Nebiolo
1,991 Pointsgiving a hand die_class append
I don't understand why in the for loop the appended value is die_class(). In my idea it should be:
for item in range(size):
self.append(item)
Can someone help me clarify this?
[MOD: added ```python formatting -cf]
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsThe goal is to append a number of instances of die_class based on the value of size.
Whether the for loop variable is called item or if the placeholder name of _ (an underscore) is used, its value will be range from zero to (size - 1).
We agree that the for loop will need to cycle a size number of times. It's the action inside the loop that matters.
Appending item will append the integer value of item in each loop.
Instead, we want a die_class instance appended. Since, in this case we aren't actually using the value of item inside the loop, it is common to use an underscore as the loop variable to indicate the loop variable value will not be used.
for _ in range(size):
self.append(die_class())
Post back if you need more help. Good luck!!!
Stefano Nebiolo
1,991 PointsStefano Nebiolo
1,991 PointsThank for the answer. Nevertheless, I still don't understand why we append die_class(). Doing so, we are appending the die class we are using and not the die number we got.
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsWhen using
die_class()with parens, the class passed in is called like a function. This call will return an instance ofdie_classwhich will have the die value initialized by its__init__method.