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

Oliver White
5,961 PointsUnsure the solution for Yahtzee part two
Hello
I have just had an operation and missed a month of the world -- I was in a bad position in the course to step away for so long.
Questions
Do i need to use super or as it is a class method and has the parent class defined will it know to run that ?
For the variable as it is a list do I need to create/declare a list and then name it or as it is expecting that is creating a variable on its own ok ?
Do i need to iterated over every dice rolled ?
When returning the class do I provide the variable value ( num_dice) or the class method or the parent class ?
Am I miles away ?
Sorry -- I am not sure how far into this my logic falls away -- first time looking at pyhton in a month - thanks
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):
super().__init__(sides=20)
from dice import D20
class Hand(list):
@property
def total(self):
return sum(self)
@classmethod
def roll(cls,num_dice):
my_hand = Hand.D20(num_dice)
return cls(roll)
5 Answers

Steven Parker
242,296 PointsA class has access to its parent methods unless it overrides them. For example, if you redefine __init__()
in your extended class, you would need to call super().__init__()
from within it to access the parent version.
When you create a "Hand" object, it is a list, so you can do any list operation (such as "append") directly on it.
You will likely want to loop using the count to create a list of the proper size.
Since you want to return the specific class object you created, you will use the variable name.
Happy pyhton-ing.

Oliver White
5,961 PointsI think i got a bit further -- sorry been such a gap
I think I am nearly there --
from dice import D20
class Hand(list): @property def total(self): return sum(self)
@classmethod
def roll(cls,num_dice):
my_hand = cls()
for _ in range(num_dice):
my_hand.append.(D20())
return my_hand

Oliver White
5,961 PointsSorry -- I think I have put them in order
It still doesnt like it --
from dice import D20
class Hand(list): @property def total(self): return sum(self)
@classmethod
def roll(cls,num_dice):
my_hand = cls()
for _ in range(num_dice):
my_hand.append.(D20())
return my_hand

Oliver White
5,961 Pointssorry - I think I made it worse
from dice import D20
class Hand(list): @property def total(self): return sum(self)
@classmethod
def roll(cls,num_dice):
my_hand = []
for _ in range(num_dice):
my_hand.append.(D20())
return cls(my_hand)

Oliver White
5,961 PointsI had an extra "." between append and the D20 part --
my_hand.append.(D20())
Its is hard to spot errors when it rejects the whole page
Thanks for the help
Steven Parker
242,296 PointsSteven Parker
242,296 PointsYes, you mostly have it, with one remaining issue. The
return
indentation places it inside the loop, which will cause the method to exit during the first iteration. Instead, it should line up with thefor
to make it occur after the loop is complete.