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 trialBen Hedgepeth
Python Web Development Techdegree Student 10,287 Pointsclass method "Roll" won't return instance
Not sure where I'm going wrong with this code. With the method I'm just returning an instantiation of the class in question. All of the code to produce n
dice is in the __init__
method.
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)
class Hand(list):
def __init__(self, size=2, dice_type=None):
if not dice_type:
raise TypeError
super.__init__()
for _ in range(size):
self.append(dice_type())
@classmethod
def roll(cls, size, di=D20):
return cls(size)
@property
def total(self):
return sum(self)
2 Answers
Ben Hedgepeth
Python Web Development Techdegree Student 10,287 PointsI don't know if I'm doing this right. I'm getting Bummer: Can't get the length of a
Hand``
from dice import D20
class Hand(list):
def __len__(self):
return super().__len__()
@classmethod
def roll(cls, size=2, dice_type=D20):
for _ in range(size):
cls.append(dice_type())
return cls()
@property
def total(self):
return sum(self)
OR
@classmethod
def roll(cls, num=2, dice_type=D20):
hand_round = cls()
for _ in range(num):
hand_round.append(dice_type)
return hand_round
Steven Parker
231,275 PointsYou don't have to override __init__
; 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 "dice_type") would break that.
And don't forget "from dice import D20
".
Ben Hedgepeth
Python Web Development Techdegree Student 10,287 PointsThere are several things I don't understand. The challenge says to update Hand
. As I understand a generic Hand
class just returns a [ ]
(however I don't understand why that it is not a Hand instance) How would I create an instance with two dices of a certain type of D20
without overriding __init__
?
When it comes down to it, I guess I don't understand what's going on. Kenneth does an awful job covering subclassing built-ins in my opinion.
Steven Parker
231,275 PointsWhat I'm suggesting is that "roll" can potentially do all the work - creating a "Hand" instance, loading it with some number of D20's, and then returning it.
If you were to implement "roll" this way, you would not need a new "__init__
".
Steven Parker
231,275 PointsSteven Parker
231,275 PointsYou nearly had it there on that 2nd example! You just forgot to put parentheses after "dice_type" when you append it:
hand_round.append(dice_type())
Also, you don't need to override "
__len__
', the "parent's version is inherited by default.