Python Object-Oriented Python Dice Roller RPG Roller

Idris Abdulwahab
PRO
Idris Abdulwahab
Pro Student 2,902 Points

hands.py

I'm definitely missing something vital in my code. I tried to write a function for roll using super() to override the arguments in the init method for Hand, so that D20 method can be passed in from the dice class. But it's not working out. Please help. Thank you.

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

class Hand(list):
    def__init__(self, new_roll = None, *args, **kwargs):
        super().__init__()
        self.append(new_roll)

    def roll(self, number):
        super().__init__(new_roll = D20())
        for _ in range(number):
            return self

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

3 Answers

Steven Parker
Steven Parker
163,023 Points

You don't have to override __init__ to complete this challenge; 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 "new_roll") would break that.

Idris Abdulwahab
Idris Abdulwahab
Pro Student 2,902 Points

@Steven Parker: Thank you for your response but I got confused so I refactored it as below but still stuck in the woods. Please help further.

from dice import D20

class Hand(list):
    def __init__(self, size = 0, new_roll = None, *args, **kwargs):
        super().__init__()
        for _ in range(size):
            self.append(new_roll())


    def roll(self, number):
        super().__init__(size = number, new_roll = D20)
        return self


    @property
    def total(self):
        return sum(self)
Steven Parker
Steven Parker
163,023 Points

Good step making the new instance parameters optional, but there's a few other issues to resolve. Here's some hints:

  • "roll" won't need to call __init__ explicitly, just create a new instance
  • "roll" needs to create a new instance
  • it might work well to have "roll" be a classmethod
  • you can only "return" once, it doesn't make sense to do it in a loop
  • the loop might work better with the "append"
Steven Parker
Steven Parker
163,023 Points

You're really close now, but 'cls" in the classmethod represents "Hand", so you wouldn't want to pass a "Hand" to itself. So just pass it the size directly:

        return cls(size)
Idris Abdulwahab
PRO
Idris Abdulwahab
Pro Student 2,902 Points

@Steven Parker: Thank you for your useful hints. I tried as below but error message reads - Bummer: Can't get the length of a Hand. Please help to look it up.

from dice import D20

class Hand(list):
    def __init__(self, size = 0, new_roll = D20, *args, **kwargs):
        super().__init__()
        for _ in range(size):
            self.append(new_roll())

    @classmethod
    def roll(cls, size):
        return cls(Hand(size))

    @property
    def total(self):
        return sum(self)
Steven Parker
Steven Parker
163,023 Points

See the comment I added to my answer.

Idris Abdulwahab
PRO
Idris Abdulwahab
Pro Student 2,902 Points

@Steven Parker: Thank you so much for your patient insights and hints on that code challenge. You helpful hint worked and I was really very happy. Thank you once again.