Really need help - how do I fix the "unsupported operand type" error for my program? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

25th Dec 2021, 7:47 PM
Conor Climo
Conor Climo - avatar
5 Answers
+ 3
Conor Climo Reread my answer carefully, restore your original code and change only "add" and "gt" in it.
25th Dec 2021, 8:38 PM
Solo
Solo - avatar
+ 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".
25th Dec 2021, 8:06 PM
Solo
Solo - avatar
+ 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)
26th Dec 2021, 6:19 AM
Solo
Solo - avatar
0
Now I'm getting "NotImplemented" as a result. Ugh. I am using gt just as add.
25th Dec 2021, 8:30 PM
Conor Climo
Conor Climo - avatar
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.
25th Dec 2021, 8:48 PM
Conor Climo
Conor Climo - avatar