0
Really need help - how do I fix the "unsupported operand type" error for my program?
Just have been doing the Operator Overload Class and I have hit a snag w/ respects to a program (which I have added here) - I mainly keep getting the "unsupported operand type" error despite the program being decently written. https://code.sololearn.com/cyUblyU50LG6/?ref=app
5 Antworten
+ 3
Conor Climo
Reread my answer carefully, restore your original code and change only "add" and "gt" in it.
+ 3
Conor Climo
This is based on what you wrote:
result=s1.add(s2)
print(result.area())
And if you want "s1 + s2" you need to change "add" to "__add__"
With "gt" exactly the same as with "add".
+ 2
Conor Climo, unfortunately you did not understand.
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 self.area() > other.area()
w1 = int(input())
h1 = int(input())
w2 = int(input())
h2 = int(input())
s1 = Shape(w1, h1)
s2 = Shape(w2, h2)
result = s1 + s2
print(result.area())
print(s1 > s2)
0
Now I'm getting "NotImplemented" as a result. Ugh. I am using gt just as add.
0
Hey! I just figured out the issue w/ my code! I didn't use the Dunders when I was defining __add__ and __gt__ in my program. Thanks for pointing me in the right direction.