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 trialLeo Marco Corpuz
18,975 PointsComparison challenge
Am I missing anything in this challenge?
class Song:
def __init__(self, artist, title, length):
self.artist = artist
self.title = title
self.length = length
def __eq__(self,other):
return int(self.length)==other
def __lt__(self,other):
return int(self.length)<other
def __gt__(self,other):
return int(self.length)>other
def __lte__(self,other):
return int(self.length)<other or int(self.length)==other
def __gte__(self,other):
return int(self.length)>other or int(self.length)==other
3 Answers
Steven Parker
231,269 PointsIt looks like you missed the last part of the instructions: "Probably a good idea to be able to convert Songs to ints, too, huh?" I take that as a hint that you may need an "__int__
" method.
A few other issues:
- you won't need to explicitly refer to the "length" property once you have the
__int__
method - the method names of
__lte__
and__gte__
should be__le__
and__ge__
- you can use the "<=" and ">=" operators to simplify the inequality tests
pet
10,908 PointsYour missing the
def __int__(self):
above your __ eq __ method.
Other than that you should be good :)
Rutendo Chidewu
3,529 Pointsclass Song: def init(self, artist, title, length): self.artist = artist self.title = title self.length = length
def __int__(self):
return self.length
def __eq__(self, other):
return int(self) == other
def __gt__(self, other):
return int(self) > other
def __lt__(self, other):
return int(self) < other
def __ge__(self, other):
return int(self) > or int(self) == other
def __le__(self, other):
return int(self) < or int(self) == other
Rutendo Chidewu
3,529 Pointsi dont know what im missing
Steven Parker
231,269 PointsIf the answer for Leo isn't what you need, try starting a fresh question of your own.
Leo Marco Corpuz
18,975 PointsLeo Marco Corpuz
18,975 PointsWhen we add the int method, weβre just returning self.length. I donβt understand how this is converting the value to an integer. Also for the comparison methods, when we put int(self), is this referring to the int method since weβre trying to get self.length?
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThe length is the value. What the method does is allow the conversion of a Song (which is an object) into an integer value.
pet
10,908 Pointspet
10,908 PointsQuite correct Steven :)