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

Dmitriy Ignatiev
PLUS
Dmitriy Ignatiev
Courses Plus Student 6,236 Points

RPG Roller

I passed the challeng but still doesn't undestand the following, could you please clarify:

  1. How argument num_dice has conection with arguments instantiated in init method and replace them when we call def roll?

  2. Why it 's print out object instead of value in roll and sum (we putted str method but it doesn't work in Pycharm) and it is still print out objects, how it can be solved?

[<dice_1.D20 object at 0x1014247b8>, <dice_1.D20 object at 0x101424860>] instead of figures [1, 12]

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, size = 2, die_class = D20):
        super().__init__(self)

        for _ in range(size):
            self.append(die_class())

    @classmethod 
    def roll (cls, num_dice):
        return cls(num_dice)


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

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

How argument num_dice has conection with arguments instantiated in init method and replace them when we call def roll?

When you call Hand.roll(5), that's the same as calling Hand(5), which would also be equivalent to Hand(size=5). Since both keyword arguments have a default value, any that don't have a value provided just use the default.

Why it 's print out object instead of value in roll and sum (we putted str method but it doesn't work in Pycharm) and it is still print out objects, how it can be solved?

I'm not sure what you're printing here (and I don't see any __str__ methods). If you're printing a Hand instance, though, that's just using list.__str__ (since it extends list) which will use the __repr__ output for each thing in the list. If your Die class doesn't have a __repr__ method, you'll get what you showed there, a class name and memory location.