+ 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