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 trialcb123
Courses Plus Student 9,858 PointsD20 Dice Challenge: Need help understand length issue, and positional argument 'rolls'
Within the challenge I am getting some error message about not getting the length of roll, but I do not understand how to resolve.
From workspaces I tried to simulate the expected request so as to troubleshoot the issue from console
>> from hands import Hand
>> Hand.roll(2)
and i get the below error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: roll() missing 1 required positional argument: 'rolls'
As I provided an argument for roll() and my method accounts for rolls, what am I missing?
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):
@property
def total(self):
return sum(self)
def roll(self, rolls):
for _ in range(rolls):
self.append(D20())
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsThis challenge is a bit harder. Since the execution will be from the class Hand
, a regular method will not work since an instance of the method hasn't been created yet. Best to use a classmethod. Since the challenge wants an instance returned, this classmethod should explicitly return an instance.
- Use a @classmethod decorator. Code style suggests using "cls" in place of the usual "self"
- Create an instance. Can be as simple as
self = cls()
- Your for loop should work OK as-is
- return
self
as the instance
Post back if you need more help. Good luck!!