Intermediate Python - 22.2 Practice Help - “Shape Factory” | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Intermediate Python - 22.2 Practice Help - “Shape Factory”

We are improving our drawing application. Our application needs to support adding and comparing two Shape objects. Add the corresponding methods to enable addition + and comparison using the greater than > operator for the Shape class. The addition should return a new object with the sum of the widths and heights of the operands, while the comparison should return the result of comparing the areas of the objects. Please help, thanks! Code in comments.

12th Jul 2021, 4:21 PM
Zach Z
24 Answers
+ 6
Sorry, Calvin... here it's the complete code: 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.width * self.height > other.width * other.height
6th Aug 2021, 7:52 PM
Kleber Castro
Kleber Castro - avatar
+ 6
In class Shape u need to define the magic method __add__ With parameters self and other Within create a new instance of shape like demanded and return it. the two heights are self.height and other.height
12th Jul 2021, 5:02 PM
Oma Falk
Oma Falk - avatar
+ 2
class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width*self.height 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)
12th Jul 2021, 4:22 PM
Zach Z
+ 2
Zach Z You may revisit the concept of magic methods in Python. Also, the question is a bit vague.
12th Jul 2021, 5:04 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Zach Z Here's the solution: class Shape: def __init__(self, x, y): self.length = x self.width = y def __add__(self, obj): return Shape(self.length + obj.length, self.width + obj.width) def __gt__(self, obj): return self.length * self.width > obj.length * obj.width # Hope this helps
18th Jul 2021, 4:22 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Thanks Calvin Thomas but that doesnt work either? Not sure what im doing wrong here?
20th Jul 2021, 7:21 PM
Zach Z
+ 2
class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width*self.height def __mul__(self,other): return ((self.width+other.width)*(self.height+other.height)) def __ge__(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) print(s1.area() > s2.area())
10th Sep 2022, 11:28 PM
Victor Vidal
Victor Vidal - avatar
+ 2
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.width * self.height > other.width * other.height 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)
15th Nov 2022, 6:39 PM
Guy Martial KEYOU
+ 1
Thanks Calvin Thomas i couldnt get that to work. Not sure what im going wrong here. Any insignt?
18th Jul 2021, 4:00 PM
Zach Z
+ 1
What you sent last has no output. What shoukd the full code be?
21st Jul 2021, 8:50 PM
Zach Z
+ 1
Oh, I had thought that you were struggling to make just the class. Add this code to complete it: A = Shape(int(input()), int(input())) B = Shape(int(input()), int(input())) print((A + B).area()) print(A > B) # Hope this helps
22nd Jul 2021, 2:03 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas i tried combining the code from your two comments but still doesnt work. Can you post the full code or at least comments in order of it ? Thanks!
28th Jul 2021, 8:35 PM
Zach Z
+ 1
Zach Z Here you go: class Shape: def __init__(self, x, y): self.length = x self.width = y def __add__(self, obj): return Shape(self.length + obj.length, self.width + obj.width) def __gt__(self, obj): return self.length * self.width > obj.length * obj.width A = Shape(int(input()), int(input())) B = Shape(int(input()), int(input())) print((A + B).area()) print(A > B) # Hope this helps P.S. I'm sorry for the delay in getting the correct answer, as I can't see the question.
29th Jul 2021, 1:44 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Since the method "area" is defined, why can't I use the code below? def __gt__(self, other): return self.area > other.area Is this not the same as this code? def __gt__(self, other): return self.width * self.height > other.width * other.height
6th Aug 2021, 6:29 PM
Kleber Castro
Kleber Castro - avatar
0
Calvin Thomas ya i dont always find the questions clear or always clearly related to the previous lesson. Appreciate that Oma Falk Can you give more direction, not sure what to do next. Thanks
13th Jul 2021, 9:09 PM
Zach Z
0
Zach Z Here's a code with 3 magic methods: class Complex: def __init__(self, x, y): self.real = x self.imag = y def __add__(self, *p): a = self.real b = self.imag for x in p: a += x.real b += x.imag return Complex(a, b) def __str__(self): a = self.real b = self.imag return str(a) * bool(a) + f" {'+' * (b > 0) or '-'} " * bool(a and b) + f"{abs(b)}i" * bool(b) A = Complex(1, 2) B = Complex(4, 2) C = Complex(9, 2) print(A + B + C) # 14 + 6i Magic methods are special methods that are can be called without the parentheses format. Here, the "__init__" magic method is called whenever the Complex objects are created. The "__add__" method is called whenever two such objects are added with each other. Finally, the "__str__" method is called whenever an instance of the class "Complex" gets printed. As you can clearly see, these magic methods are bound by double-underscores on either side ("dunder" methods).
14th Jul 2021, 3:30 AM
Calvin Thomas
Calvin Thomas - avatar
0
The "__add__" and the "__gt__" magic methods are to be used here. I hope that this helps.
14th Jul 2021, 3:31 AM
Calvin Thomas
Calvin Thomas - avatar
0
Zach Z What's the error shown? How many test cases are passed?
21st Jul 2021, 1:43 AM
Calvin Thomas
Calvin Thomas - avatar
0
Calvin Thomas no worries or need to apologize, appreciate the help! Still not working though lol. Here is the orignal code again. 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 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)
30th Jul 2021, 5:22 PM
Zach Z
0
Zach Z Damn! I bet that I'll help you with this one. Let me just take a look at the question myself. Edit: It seems that I won't be able to skip to the code coach in the Intermediate Python course without completing each of the lessons.
30th Jul 2021, 5:35 PM
Calvin Thomas
Calvin Thomas - avatar