0

Python — classes and objects — error — help

I need help with the task from the course python for intermediate — OOP — Lesson 3. I have tried the code from the solution, but it is not working. It says I have an error, however the code is the same as provided for the solution. My code as follows: class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width*self.height #your code goes here 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) Error message: Unsupported operand type for +: Shape and Shape

31st Dec 2022, 12:41 AM
Lamron
Lamron - avatar
3 Antworten
+ 4
The add method should be written like this "__add__"
31st Dec 2022, 3:08 AM
Ab SH
+ 4
The magic methods are also commonly referred to as "dunder methods", this term hints on the fact that their names are enclosed by double underscore characters. __init__ __add__ __gt__ These underscores are not just stilistic highlights, they are mandatory parts of the name.
31st Dec 2022, 6:48 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks people
31st Dec 2022, 1:11 PM
Lamron
Lamron - avatar