0
It gives an erorr saying that __init__ is missing the argument 'h' in __gt__ although it's not missing! Some explain please.
class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width*self.height def __add__(self, other): return Shape(self.width + other.width, self.height + other.height) def __gt__(self, other): return Shape(self.width * self.width > other.height * other.height) w1 = 1 h1 = 2 w2 = 3 h2 = 4 s1 = Shape(w1, h1) s2 = Shape(w2, h2) result = s1 + s2 print(result.area()) print(s1 > s2)
6 odpowiedzi
+ 1
Intermediate Depression if you put the comparison inside Shape(), Python interprets this as creating a new shape... Shape(0,) or Shape(1,)
The boolean result ( either 1 for True or 0 for False) is used as the first argument w, and there is no h argument, that is why there is the error msg about init missing h.
+ 3
your __gt__ should return a boolean, not a Shape.
maybe you want to compare areas, then it should be
def __gt__(self, other):
return (self.width*self.height)>(other.width*other.height)
+ 2
Well done Bob_Li , you beat me to it 🤣👍
+ 2
Rik fast fingers > fat fingers 😁
0
Bob_Li
Oh no you didn't get me, i already know how to fix the code, that would be.
def __gt__(self, other):
return self.area() > other.area()
I was just wondering what is wrong with "shape(slef.width * self.height > other.width * other.height"
Why does it return an erorr ?
0
Bob_Li TYSM