Intermediate Python - 22.2 Practice Help - “Shape Factory” | Sololearn: Learn to code for FREE!

+2

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.

7/12/2021 4:21:54 PM

Zach Z

22 Answers

New Answer

+4

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

+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

+3

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)

+2

Zach Z You may revisit the concept of magic methods in Python. Also, the question is a bit vague.

+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

+1

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).

+1

Thanks Calvin Thomas i couldnt get that to work. Not sure what im going wrong here. Any insignt?

+1

Thanks Calvin Thomas but that doesnt work either? Not sure what im doing wrong here?

+1

What you sent last has no output. What shoukd the full code be?

+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

+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!

+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.

+1

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)

+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

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

0

The "__add__" and the "__gt__" magic methods are to be used here. I hope that this helps.

0

Zach Z What's the error shown? How many test cases are passed?

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.

0

Zach Z Just type out the expected results for a test case as an example.

0

Kleber Castro Where is 'area' defined?