Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Dice Roller RPG Roller

Oliver White
Oliver White
5,961 Points

Unsure the solution for Yahtzee part two

Hello

I have just had an operation and missed a month of the world -- I was in a bad position in the course to step away for so long.

Questions

Do i need to use super or as it is a class method and has the parent class defined will it know to run that ?

For the variable as it is a list do I need to create/declare a list and then name it or as it is expecting that is creating a variable on its own ok ?

Do i need to iterated over every dice rolled ?

When returning the class do I provide the variable value ( num_dice) or the class method or the parent class ?

Am I miles away ?

Sorry -- I am not sure how far into this my logic falls away -- first time looking at pyhton in a month - thanks

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):
    @property
    def total(self):
        return sum(self)

    @classmethod
    def roll(cls,num_dice):
        my_hand = Hand.D20(num_dice)
               return cls(roll) 

5 Answers

Steven Parker
Steven Parker
242,296 Points

A class has access to its parent methods unless it overrides them. For example, if you redefine __init__() in your extended class, you would need to call super().__init__() from within it to access the parent version.

When you create a "Hand" object, it is a list, so you can do any list operation (such as "append") directly on it.

You will likely want to loop using the count to create a list of the proper size.

Since you want to return the specific class object you created, you will use the variable name.

Happy pyhton-ing. :smirk:

Steven Parker
Steven Parker
242,296 Points

Yes, you mostly have it, with one remaining issue. The return indentation places it inside the loop, which will cause the method to exit during the first iteration. Instead, it should line up with the for to make it occur after the loop is complete.

Oliver White
Oliver White
5,961 Points

I think i got a bit further -- sorry been such a gap

I think I am nearly there --

from dice import D20

class Hand(list): @property def total(self): return sum(self)

@classmethod
def roll(cls,num_dice):
   my_hand = cls()
   for _ in range(num_dice):
        my_hand.append.(D20())
        return my_hand
Oliver White
Oliver White
5,961 Points

Sorry -- I think I have put them in order

It still doesnt like it --

from dice import D20

class Hand(list): @property def total(self): return sum(self)

@classmethod
def roll(cls,num_dice):
    my_hand = cls()
    for _ in range(num_dice):
        my_hand.append.(D20())
    return my_hand
Oliver White
Oliver White
5,961 Points

sorry - I think I made it worse

from dice import D20

class Hand(list): @property def total(self): return sum(self)

@classmethod
def roll(cls,num_dice):
    my_hand = []
    for _ in range(num_dice):
        my_hand.append.(D20())
    return cls(my_hand)
Oliver White
Oliver White
5,961 Points

I had an extra "." between append and the D20 part --

my_hand.append.(D20())

Its is hard to spot errors when it rejects the whole page

Thanks for the help