+ 1

How do you solve the magic method “shape” challenge on python intermediate module OOO?

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. I had to delete my attempt at the question due to length requirements. See below it.

30th Mar 2021, 4:05 AM
Fan Mason
10 Antworten
+ 9
I made it just! 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())
30th Apr 2021, 2:42 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
+ 3
Did you change the area() method of the Shape class? def area(self): return self.width * self.height Change __ge__ to __gt__ and just return other.area() > self.area() I don't see anything that uses the > greater than magic method either. So, hopefully you didn't change the provided code too much.
30th Mar 2021, 5:17 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
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 = Shape(w1+w2,h1+h2) print(result.area()) print(s1 > s2)
18th Nov 2022, 7:33 AM
Nasser Ashrafpour
Nasser Ashrafpour - avatar
+ 2
My attempt class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self, other): return Shape(self.width*other.width,self.height*other.height) #your code goes here def __add__(self, other): return Shape(self.width+other.width,self.height+other.height) def __ge__(self, other): return Shape(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())
30th Mar 2021, 5:07 AM
Fan Mason
+ 1
Please Provide The Question So Non Pro User Can Also Help
30th Mar 2021, 4:26 AM
Hacker Badshah
Hacker Badshah - avatar
+ 1
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): if self.area() > other.area(): return True else: return False 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)
29th Aug 2021, 1:56 PM
Anthony Amabile
Anthony Amabile - avatar
+ 1
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 = Shape(s1, s2) print(result.area()) print(s1.area() > s2.area())
21st Jul 2022, 8:12 PM
Peer Mogensen
0
Did any one find this any help about the right one thank you "am also stacked with this Project"
13th Jan 2023, 6:41 AM
Khomotso Mokgapa
Khomotso Mokgapa - avatar
0
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()
27th Aug 2023, 10:42 PM
mohamad eldimardash
mohamad eldimardash - avatar
0
here is my code ! is enough only 4 code line add to the example code class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width*self.height #my code is added def __add__(self,other): return Shape(self.width+other.width , self.height+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 #my code is added print(result.area()) print(s1.area()>s2.area())
11th Dec 2023, 10:41 AM
vahid kamrani
vahid kamrani - avatar