+ 2

Fellow python guys am kinda out here cause am only getting the last 3 correct but the first one is challenging me

Operator Overloading 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. The given code creates two Shape objects from user input, outputs the area() of their addition and compares them. HERE IS MY CODE PLIZ: 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): if Shape(self.width>other.width, self.height>other.height): return True 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)

20th Jan 2024, 10:29 AM
MUWANGUZI PETER
MUWANGUZI PETER - avatar
24 Answers
+ 5
Did you check the inputs and output? We know the inputs are 2, 3, 4, 2. Your output is 30 / True, but the expected output is 30 / False. It suggests an overloading is returning a wrong answer, and it is the __gt__ method. As the question goes, "the comparison should return the result of comparing the **areas** of the objects". And now re-examine your code. What is it comparing?
20th Jan 2024, 1:11 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
MUWANGUZI PETER , One thing I forgot to say about your __gt__ method is that you don't need to create a new object in there. Just compare the areas of self and other, and return the bool.
20th Jan 2024, 7:55 PM
Rain
Rain - avatar
+ 3
Shubham Karjee comparing sides separately is not the same as comparing areas. areas don't work that way. a shape can have width or height smaller or equal to another, but if the other value is big enough, the area would still be greater than the other. Your if condition will fail here. w1 = 1 h1 = 7 w2 = 2 h2 = 3 area1 = w1 * h1 print('area1 =', area1) area2 = w2 * h2 print('area2 =', area2) #correct: proper area comparison print('area1 > area2 :', area1>area2) #True #incorrect: separate widths and heights comparison print('(w1>w2) and (h1>h2) :', (w1>w2) and (h1>h2)) #False
21st Jan 2024, 10:27 PM
Bob_Li
Bob_Li - avatar
+ 2
Try to use the search function... https://www.sololearn.com/discuss/3244957/?ref=app
20th Jan 2024, 11:08 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Bob_Li , Hints, bro. I feel sure he would have gotten it. But since you popped it, there's a more pythonic way. def __gt__(self, other): return self.area() > other.area()
20th Jan 2024, 11:29 PM
Rain
Rain - avatar
+ 2
Rain you're right. I guess I was getting impatient... I tried to retain the same code format. But yes, directly returning the area comparison is the more Pythonic way to write it. But looking at: if Shape(self.width>other.width, self.height>other.height) made me think he's quite far off. booleans evaluate to 0 or 1. Shape(0,0), Shape(0,1), Shape(1,0) or Shape(1,1) all makes no sense in the context of the problem. Also, it is not the way to compare areas. And if Shape(w, h) always evaluate to True.
21st Jan 2024, 12:00 AM
Bob_Li
Bob_Li - avatar
+ 1
def __gt__(self, other): if Shape(self.width>other.width, self.height>other.height): return True else: return False
20th Jan 2024, 11:49 AM
noteve
noteve - avatar
+ 1
MUWANGUZI PETER , Thanks. I see. You haven't fixed your __gt__ method. Reread Wong Hei Ming's comment about that. When you fix it, remove the empty line under the def line, just for aesthetics, like your other methods. Tip for comments: You can address a comment to a person by typing @ and choosing their name from the popup list. That highlights their name, turns it into a link, and sends them an alert, so they know to come take a look.
20th Jan 2024, 7:45 PM
Rain
Rain - avatar
+ 1
MUWANGUZI PETER Take a careful look at the problem. The > overload is supposed to compare areas. Your comparison should be def __gt__(self, other): if self.area()>other.area(): return True return False
20th Jan 2024, 11:14 PM
Bob_Li
Bob_Li - avatar
+ 1
Rain Writing in pythonic way takes time and practice. It may not be obvious to someone new to programming. Bob_Li Maybe OP *is* thinking like: If area of A is bigger than area of B, A's width and height must greater than B's.
21st Jan 2024, 12:19 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Bob_Li , Wong Hei Ming , Probably just a copy-paste-forgot-to-modify error from the _add_ method. I used to do that. You think you're saving typing time, but if you count debugging, it's faster to start from scratch.
21st Jan 2024, 1:04 AM
Rain
Rain - avatar
+ 1
MUWANGUZI PETER , You're still attempting to create a new object with capitalized Shape(blablabla). That's why you're getting the __init__ positional argument error. Don't create a new object. And don't recalculate area in the __gt__ method. Make it call the area method you already have. Return true if self's area is greater than other's area. Otherwise, False. If you get desperate, the answer is in the thread already.
21st Jan 2024, 12:02 PM
Rain
Rain - avatar
+ 1
The code provided by the playground has their usefulness. Try to read the description and the code provided, and think about how they are linked.
21st Jan 2024, 12:31 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Here, in __gt__ area, apply this: def __gt__(self, other): if (self.width > other.width) and (self.height > other.height): return True else: return False
21st Jan 2024, 6:56 PM
SHUBHAM KARJEE
SHUBHAM KARJEE - avatar
+ 1
Then we can try: def __gt__(self, other): If (self.width*self.height)>(other.width*other.height): return True else: return False
24th Jan 2024, 6:26 PM
SHUBHAM KARJEE
SHUBHAM KARJEE - avatar
0
Thank u but still the first one is not working I just don't know why
20th Jan 2024, 11:56 AM
MUWANGUZI PETER
MUWANGUZI PETER - avatar
0
MUWANGUZI PETER , Post your updated code.
20th Jan 2024, 7:22 PM
Rain
Rain - avatar
0
Rain here it is please help me: 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): if Shape(self.width>other.width, self.height>other.height): 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)
20th Jan 2024, 7:35 PM
MUWANGUZI PETER
MUWANGUZI PETER - avatar
0
Wong Hei Ming 😁 areas don't work that way. And then there is the always True if condition...
21st Jan 2024, 12:25 AM
Bob_Li
Bob_Li - avatar
0
Rain morning I have tried to change by multiplying the variables but still tells me that the __init__ method misses one positional argument
21st Jan 2024, 8:28 AM
MUWANGUZI PETER
MUWANGUZI PETER - avatar