Python Object-Oriented Python Dice Roller RPG Roller

Pitrov Secondary
Pitrov Secondary
5,118 Points

I am lost on task 2

I have not Idea of what is wrong. Anyone help?
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):
from dice import D20
class Hand(list):
    def __init__(self, size=2, dice_type=None):
        if not dice_type:
            raise TypeError

        for _ in range(size):

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

        return hand_round

    def total(self):
        return sum(self)

1 Answer

Alex Koumparos
Alex Koumparos
Python Web Development Treehouse Moderator 31,928 Points

Hi Pitrov,

In your Hand constructor you specify:

def __init__(self, size=2, dice_type=None):
        if not dice_type:
            raise TypeError

Then in your roll class method you do this:

 hand_round = cls()

Which attempts to instantiate an instance of Hand with the default arguments (size=2 and dice_type=None).

The first line of the constructor checks whether dice_type exists (i.e., is not None) and then raises.

If you fix that by passing your class method call dice_type as an argument, you will no longer get a crash but you will hit the other problem: you create two more dice than the number you intend.

Hope that points you in the right direction.