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 trial

Python Object-Oriented Python Dice Roller Compare and Contrast

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Comparison challenge

Am I missing anything in this challenge?

songs.py
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
Steven Parker
231,198 Points

It 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
Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

When 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
Steven Parker
231,198 Points

The length is the value. What the method does is allow the conversion of a Song (which is an object) into an integer value.

pet
pet
10,908 Points

Quite correct Steven :)

pet
pet
10,908 Points

Your missing the

def __int__(self):

above your __ eq __ method.

Other than that you should be good :)

class 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

i dont know what im missing

Steven Parker
Steven Parker
231,198 Points

If the answer for Leo isn't what you need, try starting a fresh question of your own.