+ 1

Juice Maker

class Juice(): def __init__(self, name, capacity): self.name = name self.capacity = capacity def __str__(self): return (self.name + ' ('+str(self.capacity)+'L)') def __add__(self,other): return self.name+"&"+other.name+ '('+str(self.capacity+other.capacity)+'L'+")" a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) #b=Juice('Banana',3.5) result = a + b print(result) my output : Orange&Apple(3.5L) expected output: Orange&Apple (3.5L) What is wrong with my code? I got the same expected result but it shows wrong answer. ------------------------------------------------------------------------------------------- Another attempt : class Juice: def __init__(self, name, capacity): self.name = name self.capacity = capacity def __str__(self): return (self.name + ' ('+str(self.capacity)+'L)') def __add__(self,other): y=self.name+"&"+other.name z='('+str(self.capacity+other.capacity)+'L'+")" return y.__add__(z) a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) result = a + b print(result)

13th Mar 2021, 12:43 AM
Monir Farag
Monir Farag - avatar
12 Respuestas
+ 9
class Juice: def __init__(self, name, capacity): self.name = name self.capacity = capacity def __str__(self): return (self.name + ' ('+str(self.capacity)+'L)') def __add__(self, other): return Juice(self.name + '&' + other.name,self.capacity+other.capacity) a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) result = a + b print(result)
19th Apr 2021, 12:05 PM
Mailan Savelino Caberios
Mailan Savelino Caberios - avatar
+ 7
I believe you should return a Juice object from __add__ magic method: def __add__(self,other): return new Juice(self.name+'&'+other.name,self.capacity+other.capacity); however, you said your output is 'Orange&Apple(3.5L)' and expected is 'Orange&Apple (3.5L)'... well, there's a missing space before parenthesis in the string returned by your __add__ function ^^
13th Mar 2021, 12:55 AM
visph
visph - avatar
+ 3
wow... so, my first though was right: you are expected to just return a new Juice object from your __add__ method ;P
13th Mar 2021, 1:55 AM
visph
visph - avatar
+ 2
but you have put it in the __str__ method ;)
13th Mar 2021, 1:06 AM
visph
visph - avatar
+ 1
thank you so much, it was the space that I didn't notice
13th Mar 2021, 1:05 AM
Monir Farag
Monir Farag - avatar
+ 1
What do you mean by putting it in the __str__ method?
13th Mar 2021, 1:48 AM
Monir Farag
Monir Farag - avatar
+ 1
in your __str__ method, you do not have missed the space ;)
13th Mar 2021, 1:49 AM
visph
visph - avatar
+ 1
No, that was a practice problem in sololearn asking to define magic add function giving the expected result. I didn't write the __str__ or update it
13th Mar 2021, 1:53 AM
Monir Farag
Monir Farag - avatar
+ 1
class Juice: def __init__(self, name, capacity): self.name = name self.capacity = capacity def __str__(self): return (self.name + ' ('+str(self.capacity)+'L)') def __add__(self, other): return Juice(self.name + '&' + other.name,self.capacity+other.capacity) a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) result = a + b print(result)
11th Dec 2021, 3:50 PM
RS Madhushika-s92063703-121423703
0
I just added print statement and it accepted successfully (I did intentionally to check) and I am working on the problem to solve
11th Apr 2021, 9:27 PM
Firoz Momin
Firoz Momin - avatar
0
class Juice: def __init__(self, name, capacity): self.name = name self.capacity = capacity def __str__(self): return (self.name + ' ('+str(self.capacity)+'L)') def __add__(self, other): names = self.name + "&" + other.name size = self.capacity + other.capacity return "{} ({}L)".format(names, size) a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) result = a + b print(result)
24th Jul 2022, 1:24 PM
Alex Mauricio Rodriguez
Alex Mauricio Rodriguez - avatar
- 2
class Juice: def __init__(self, name, capacity): self.name = name self.capacity = capacity def __str__(self): return self.name + " (" + str(self.capacity) + "L)" def __add__(self, other): Mix_name = "{}&{}".format(self.name, other.name) Mix_capacity = self.capacity + other.capacity return Juice(Mix_name, Mix_capacity).__str__() a = Juice("Orange", 1.5) b = Juice("Apple", 2.0) result = a + b print(result)
21st Jun 2021, 6:58 AM
Avid_Coder
Avid_Coder - avatar