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 trialCHRISTOPHER HECKER
1,865 PointsSuggestions to improve RPG roller 2/2
I found this challenge in need of some improvement. The description should say explicitly how the class is going to be called. I spent some time looking for the code hand.roll() as the description says "I'm going to use code similar to Hand.roll(2)". Why not just say, "I'll call the code with the following line: my_hand = Hand.roll(2)"?
Since challenges build on the lessons and previous challenges, the Hand class code should be consistent with that previously developed. I found the simplest solution to be
@classmethod
def roll(cls, num_dice = 2):
return cls(num_dice, die_class=D20)
while maintaining the rest of the class code (and importing D20 of course). But this solution fails, barking about not being able to take the length of the hand or something. On my local machine, the instance type is Hand and len() returns the length, no problem. Searching other discussions, I see that commenting out the self.sort() in the init allows the code checker to pass. I can't see any reason why the checker would fail on length simply since the instance is sorted.
1 Answer
Mark Chesney
11,747 PointsHere's my code. I don't get a run-time error.
from dice import D20
class Hand(list):
def __init__(self, size=0, die_class=None, *args, **kwargs):
if not die_class:
raise ValueError("Need a die_class")
super().__init__()
for _ in range(size):
self.append(die_class())
self.sort()
@classmethod
def roll(cls, num_dice=2):
return cls(num_dice, die_class=D20)
@property
def total(self):
return sum(self)
However, when I instantiate a new roll, I cannot get the total to update. In other words,
my_hand = Hand(size=2, die_class=D20)
my_hand
# random example: >>> [2, 3]
my_hand.roll(2)
# random example: >>> [8, 16]
my_hand.total
# >>> 5