Python Object-Oriented Python Dice Roller RPG Roller

Ewerton Luna
Ewerton Luna
Full Stack JavaScript Techdegree Student 13,816 Points

Why isn't this code passing the Hand challenge?

I tried this code in Pycharm and it pretty much worked, except that when I try Hand.roll(2) I keep getting AttributeError: type object 'Hand' has no attribute 'roll'

Can anyone tell me if making small changes to this code it will work? I have seen other people code to pass this challenge, but I thought that my code should pass it too.

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)
from dice import D20

class Hand(list):
    def __init__(self, size=0, die_class=None):
        super().__init__()  # I know I could check if die_class is truthy before this line, but I think the code should run fine for the purposes of the challenge.
        for _ in range(size):

    def roll(cls, size, die_class = D20):
        return cls(size, die_class)                        

    def total(self):
        return sum(self)    

1 Answer

Chris Howell
Chris Howell
Python Web Development Techdegree Graduate 49,484 Points

Hey Ewerton Luna

I copy/pasted your snippets into the challenge and they seem to pass both tasks for me.

I would try clicking the Restart button on the challenge and then paste your code back into each .py file. I initially suspected that you might a mixture of tab and space characters in your snippet. I checked for that first and your snippets seem to only be using space characters for indentation.

Give it another go, and let us know if it still doesn't pass. Also be sure you do not have any leading whitespace on the first line.

Ewerton Luna
Ewerton Luna
Full Stack JavaScript Techdegree Student 13,816 Points

Hi, @Chris! Thank you very much! I tried the code again as you said and it worked! Thank you very much!!