Help me in the oop code coach | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Help me in the oop code coach

This is my trial class Juice: def __init__(self, name, capacity): self.name = name self.capacity = capacity def __str__(self): return (self.name + ' ('+str(self.capacity)+'L)') a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) result = a + b print(result

31st Oct 2020, 12:59 PM
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active)
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active) - avatar
13 Answers
0
Implement a __add__ method as if it will be called this way a.__add__(b) It should return a new Juice object explicitly. def __add__(self, additive): return new Juice(x, y) Where x and y are the new object's name and capacity respectively.
31st Oct 2020, 2:32 PM
Ore
Ore - avatar
+ 6
I solved it 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,newJuice): self.name += "&" + str(newJuice.name) self.capacity += newJuice.capacity return self.__str__ a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) print(a.__add__(b)())
2nd Nov 2020, 6:10 PM
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active)
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active) - avatar
+ 5
Review this chapter, pay extra attention on the second slide. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2470/
31st Oct 2020, 1:03 PM
Ipang
+ 4
Ipang can you describe it
31st Oct 2020, 1:30 PM
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active)
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active) - avatar
+ 4
Ore, Eskay👑👑 it has a problem with the line 13 Invalid syntax
31st Oct 2020, 7:04 PM
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active)
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active) - avatar
+ 4
Ore it just worked
2nd Nov 2020, 6:59 PM
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active)
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active) - avatar
+ 3
<a> __add__ <b>
31st Oct 2020, 1:34 PM
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active)
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active) - avatar
+ 3
Yep
2nd Nov 2020, 7:47 PM
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active)
🥇👩‍💻 Kintu Michael Evans 🔥🔥( Active) - avatar
+ 1
Kintu Michael Evans I don't know what to describe ... What you need to add Juice <a> and Juice <b> is to implement __add__ magic method.
31st Oct 2020, 1:33 PM
Ipang
+ 1
Kintu Michael Evans that is why __add__ is called a *magic* method
2nd Nov 2020, 7:32 PM
Ore
Ore - avatar
0
There is a cheat to that problem just print the answer directly since it has only one test case, believe me it works 😁😁
31st Oct 2020, 2:57 PM
Pythøñ Cúltîst⚔️🔥🇳🇬
Pythøñ  Cúltîst⚔️🔥🇳🇬 - avatar
0
Kintu Michael Evans WTG! But replace the last line with print(a + b)
2nd Nov 2020, 6:58 PM
Ore
Ore - avatar