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 trialIdris Abdulwahab
Courses Plus Student 2,961 Pointshands.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.
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, 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
231,269 PointsYou 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
Courses Plus Student 2,961 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
231,269 PointsSee the comment I added to my answer.
Idris Abdulwahab
Courses Plus Student 2,961 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.
Idris Abdulwahab
Courses Plus Student 2,961 PointsIdris Abdulwahab
Courses Plus Student 2,961 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.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsGood step making the new instance parameters optional, but there's a few other issues to resolve. Here's some hints:
__init__
explicitly, just create a new instanceSteven Parker
231,269 PointsSteven Parker
231,269 PointsYou'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)