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 trialHenry Lin
11,636 PointsMy code doesn't pass but worked fine on workspace!!!
I don't know what is wrong with code. Can someone help?
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, size=0, dice_class=D20):
self.size = size
super().__init__()
for _ in range(size):
self.append(dice_calss())
self.sort()
@classmethod
def roll(cls, num_dice):
return cls(num_dice)
@property
def total(self):
return sum(self)
1 Answer
Steven Parker
231,261 PointsI see two issues:
- you wrote "dice_calss" instead of "dice_class"
- don't sort the Hand, I'm not sure why but it messes up the check
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsThe sorting raises a
TypeError
because the classD20
does not have a defined__cmp__
method needed by thesort()
method to make comparisons. Running in the REPL shows the issue:Steven Parker
231,261 PointsSteven Parker
231,261 PointsThat makes sense, but then how would it have worked in the workspace?
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsI would need to see a snapshot of the workspace to know. It is not clear the exact conditions where it "worked".
Henry Lin, could post a snapshot link of the workspace where this worked?
Henry Lin
11,636 PointsHenry Lin
11,636 PointsChris Freeman I have figureed it out. First, I had typo in my challenge but not in workspace. Second, I used D6 class in workspace. And I deleted the sort function, and it passed the challenge. Thanks.
Steven Parker
231,261 PointsSteven Parker
231,261 PointsWhere was "D6" defined? Did it include a "
__cmp__
" method?