Python Object-Oriented Python Dice Roller RPG Roller

Ricardo Franco
seal-mask
.a{fill-rule:evenodd;}techdegree
Ricardo Franco
Python Development Techdegree Student 13,518 Points

RPG Roller Code Challenge

My error is reading "Got the wrong length for a 'Hand()'. I am clearly getting some kind of length response with my current code which is good but I can't seem to get past that hump. Thank you, in advance, for your time and assistance.

dice.py
import random


class Die:
    def __init__(self, sides=2, value=0):
        if sides < 2:
            raise ValueError("Can't have fewer than two sides")
        if not isinstance(sides, int):
            raise ValueError("Sides must be a whole number")
        if not isinstance(value, int):
            raise ValueError("Value must be a whole number")
        self.sides = sides
        self.value = value or 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
from dice import D20


class Hand(list):
    def __init__(self, size=None):
        self.size = size

    @classmethod
    def roll(cls, dice_roll):
        size = []
        for _ in range(dice_roll):
            size.append(D20())   
        return cls(size)

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

#    def __len__(self):
#        return len(self)
Aaron Jorgensen
Aaron Jorgensen
Python Development Techdegree Graduate 13,648 Points

I tried your code and it passed. Not sure why you would be getting that message. Try restarting the challenge. If it persists, maybe post the error message here.

1 Answer

Steven Parker
Steven Parker
202,108 Points

The "roll" method itself looks good. :+1:   But two issues stand out:

  • you don't need to override "__init__"
  • don't modify the provided "total" method code