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 trialGeorge Clement
2,816 Pointsthis works for me in the IDE but the challenge keeps telling me it can't get length
from dice import Hand obj_hand = Hand() obj_hand.roll(4) obj_hand [17, 11, 3, 4] obj_hand.total() 35 obj_hand.len <method-wrapper '__len__' of Hand object at 0x0000023BC88BB8B8> obj_hand.len() 4 len(obj_hand) 4
not sure what I am missing to trigger the issue
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 clear_hand(self):
del self[:]
def total(self):
return sum(self)
def roll(self, amt_dice):
x = 1
while x <= amt_dice:
foo = D20()
self.append(int(foo))
x = x + 1
1 Answer
Alexander Davison
65,469 PointsHints:
-
roll
should be a@classmethod
-
roll
should not modify an instance, instead it should create and instance with the randomD20
s and return it - The new hand should contain
D20
instances, notint
s (it seems that you are converting theD20
s toint
s)
George Clement
2,816 PointsGeorge Clement
2,816 Pointsthanks for the comment I think I'll have to think about it some more.