1

I'm trying to write a method(it) that compares the size (area) of the rectangle with the area of another rectangle passed as a parameter:

class Rectangle: def __init__(self, x, y): self.width = x self.height = y def area(self): a = self.width * self.height return a def __it__(self,second): return self.area < second.area 

But I keep getting error:

TypeError: unorderable types: Rectangle() < Rectangle() 

I'm not to sure how to fix this problem

0

2 Answers 2

4

You had a typo. It's __lt__, not __it__, and you need to call the area() as a function unless you set that as a property.

Fixing all that...

>>> class Rectangle: ... def __init__(self, x, y): ... self.width = x ... self.height = y ... def area(self): ... a = self.width * self.height ... return a ... def __lt__(self,second): ... return self.area() < second.area() ... >>> Rectangle(1,3) > Rectangle(4,5) False 
Sign up to request clarification or add additional context in comments.

Comments

0

Area is a method, you're using it as though it's a variable. Adding parens should fix it (and if you're trying to do less than, it should be __lt__):

def __lt__(self, second): return self.area() < second.area() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.