1,865 Points

Suggestions 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

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.

Mark Chesney
Mark Chesney
11,499 Points

Here'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")

        for _ in range(size):

    def roll(cls, num_dice=2):
        return cls(num_dice, die_class=D20)

    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)
# random example: >>> [2, 3] 
# random example: >>> [8, 16]
# >>> 5