Python Object-Oriented Python Dice Roller RPG Roller

Ben Hedgepeth
Ben Hedgepeth
Python Web Development Techdegree Student 5,767 Points

class method "Roll" won't return instance

Not sure where I'm going wrong with this code. With the method I'm just returning an instantiation of the class in question. All of the code to produce n dice is in the __init__ method.

dice.py
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)
hands.py
class Hand(list):
    def __init__(self, size=2, dice_type=None):
        if not dice_type:
            raise TypeError
        super.__init__()
        for _ in range(size):
            self.append(dice_type())

    @classmethod
    def roll(cls, size, di=D20):       
        return cls(size)

    @property
    def total(self):
        return sum(self)

2 Answers

Ben Hedgepeth
Ben Hedgepeth
Python Web Development Techdegree Student 5,767 Points

I don't know if I'm doing this right. I'm getting Bummer: Can't get the length of aHand``

from dice import D20

class Hand(list):

    def __len__(self):
        return super().__len__()


    @classmethod
    def roll(cls, size=2, dice_type=D20):
        for _ in range(size):
            cls.append(dice_type())

        return cls()



    @property
    def total(self):
        return sum(self)

OR

    @classmethod
    def roll(cls, num=2, dice_type=D20):
        hand_round = cls()
        for _ in range(num):
            hand_round.append(dice_type)

        return hand_round
Steven Parker
Steven Parker
162,875 Points

You nearly had it there on that 2nd example! You just forgot to put parentheses after "dice_type" when you append it:

            hand_round.append(dice_type())

Also, you don't need to override "__len__', the "parent's version is inherited by default.

Steven Parker
Steven Parker
162,875 Points

You don't have to override __init__; but if you do, be careful not to change the instantiation signature. In particular, the system will expect to still be able to create generic "Hand" instances without providing arguments, so adding new required arguments (like "dice_type") would break that.

And don't forget "from dice import D20".

Ben Hedgepeth
Ben Hedgepeth
Python Web Development Techdegree Student 5,767 Points

There are several things I don't understand. The challenge says to update Hand. As I understand a generic Hand class just returns a [ ] (however I don't understand why that it is not a Hand instance) How would I create an instance with two dices of a certain type of D20 without overriding __init__?

When it comes down to it, I guess I don't understand what's going on. Kenneth does an awful job covering subclassing built-ins in my opinion.

Steven Parker
Steven Parker
162,875 Points

What I'm suggesting is that "roll" can potentially do all the work - creating a "Hand" instance, loading it with some number of D20's, and then returning it.

If you were to implement "roll" this way, you would not need a new "__init__".