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 trialJeff Vig
16,345 PointsStill stuck
All I'm getting is "Bummer! Try again!"
My code works, but it isn't meeting the challenge.
I created the classmethod. Each item in the list is an instance of the class dice.D20. The total property returns the sum of all the instances.
I'm not sure what else to do.
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__(20)
import dice
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())
self.sort()
@property
def total(self):
return sum(self)
@classmethod
def roll(cls, size):
return cls(size, die_class=dice.D20)
2 Answers
Steven Parker
231,236 PointsYou might be doing a bit too much.
The instructions don't say anything about sorting the contents of a "Hand". They also don't say anything about requiring a die_class argument to instantiate a "Hand". Or accepting args and kwargs.
Trim things down to just what the instructions required and I'll bet you pass.
Jeff Vig
16,345 PointsThank you so much. I was beginning to worry if I could ever make it work. It would be so much more helpful with a display of the interpreter results.
Jeff Vig
16,345 PointsJeff Vig
16,345 PointsNope. Trimmed it down. Rechecked the indentations. Still works, but not accepted.
Steven Parker
231,236 PointsSteven Parker
231,236 PointsBut you forgot to "
import dice
". Otherwise, I think you have it now.