+ 1

Anyone tell me about this code I don't have any idea to solve this!!

You are given a Juice class, which has name and capacity properties. You need to complete the code to enable and adding of two Juice objects, resulting in a new Juice object with the combined capacity and names of the two juices being added. For example, if you add an Orange juice with 1.0 capacity and an Apple juice with 2.5 capacity, the resulting juice should have: name: Orange&Apple capacity: 3.5 The names have been combined using an & symbol.

24th Oct 2020, 4:12 PM
Prerna Vijay Lohar.
Prerna Vijay Lohar. - avatar
4 ответов
+ 1
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) #this is given code
24th Oct 2020, 4:13 PM
Prerna Vijay Lohar.
Prerna Vijay Lohar. - avatar
+ 1
#answer class Juice: def __init__(self, name, capacity): self.name = name self.capacity =float(capacity) def __add__(self,ano): return self.name+'&'+ ano.name + ' (' + str(self.capacity+ano.capacity)+'L)' a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) result = a + b print(result)
1st Sep 2022, 12:20 AM
Mohamad Eskandari Nasab
Mohamad Eskandari Nasab - 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,ano): return Juice(self.name + '&' + ano.name, self.capacity + ano.capacity) a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) print(a+b)
14th Sep 2021, 6:52 PM
DEBASHIS GHOSH
24th Oct 2020, 5:16 PM
JaScript
JaScript - avatar