+ 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.
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())
+ 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.
+ 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)
+ 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())
+ 1
Please Provide The Question So Non Pro User Can Also Help
+ 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)
+ 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())
0
Did any one find this any help about the right one thank you "am also stacked with this Project"
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()
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())