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 trialDaniel Harris
7,568 PointsWhat am I supposed to do about hand length? Setting it to 0 when initializing and passing as argument for roll method...
OK, I'm getting a weird error and can't see how to fix it. Error is "can't get the length of hand." Well that's not a surprise when we're supposed to set the length with the "roll" method instead of in the init right??? I can arbitrarily set length in the init to say 0 or 5. When I run in Jupyter, the code gives me the length of the hand - 0 is still a length, so is 5. Now given how the prompt is set up, we're supposed to reset length when the roll method is called. My code correctly resets length with the roll method. So why can't the grader get length on a "Hand" instance, I have no idea!!! Here's my code
class Hand(list): def init(self, size=0, die_class=D20, *args, **kwargs): if not die_class: raise ValueError("You must provide a die class") super().init() for _ in range(size): self.append(die_class())
def roll(self, size, die_class=D20):
super().__init__()
self.size = size
for _ in range(size):
self.append(die_class())
def total(self):
return sum(self)
import random
class Die:
def __init__(self, sides=2, *args, **kwargs):
self.sides = sides
self.value = random.randint(1, sides)
def __int__(self):
return self.value
def __eq__(self, other):
return int(self) + int(other)
def __ne__(self, other):
return int(self) != int(other)
def __gt__ (self, other):
return int(self) > int(other)
def __lt__(self, other):
return int(self) < int(other)
def __ge__(self, other):
return int(self) > int(other) or int(self) == int(other)
def __le__(self, other):
return int(self) < int(other) or int(self) == int(other)
def __add__(self, other):
return int(self) + int(other)
def __radd__(self, other):
return int(self) + int(other)
def __repr__(self):
return str(self.value)
class D20(Die):
def __init__(self, value =0):
super().__init__(sides=20, value=value)
from dice import D20
class Hand(list):
def __init__(self, size=0, die_class=D20, *args, **kwargs):
if not die_class:
raise ValueError("You must provide a die class")
super().__init__()
for _ in range(size):
self.append(die_class())
def roll(self, size, die_class=D20):
self.size = size
for _ in range(size):
self.append(die_class())
def total(self):
return sum(self)
1 Answer
Steven Parker
231,248 PointsThe message may be a bit misleading, but here's a few hints:
- the challenge expects to "get back an instance of Hand...", but "roll" isn't returning anything.
- you may want to implement this as a class method
- don't repeat code already done by
__init__
Also, it looks like you added several new methods to "Die", but that's not part of the instructions. For best results with the challenges, do only what the instructions ask for.
Daniel Harris
7,568 PointsDaniel Harris
7,568 PointsThanks this helped :-)