How to solve juice maker in python!!??
I am unable to solve one of the python code ,which I have recently posted . Please check my post and anyone can help me in this
10/27/2020 5:49:43 PM
Chahat Singh
38 Answers
New AnswerYou just have to create a method which take two object mix their name and capacity and make a new object. def __add__(self,other): #Adding Name new_name = self.name+"&"+other.name #Adding Capacity new_capacity = self.capacity+other.capacity #Returning New Object return Juice(new_name,new_capacity) Hope It Helps You ๐
#Here's my solution to the #problem. Hope it'll suffice. 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)())
the following is my solution: 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)
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)
I have a question how can i put texte where i can write after run the programme in phyton
Def __add__(self,n): Self.name+="&"+str(n.name) Self.capacity+=n.capacity Return self.__str__ Print(a.__ad__(b)()
my code is a bit more roundabout than others but it worked. not as elegant but not bad for adopting pieces of others and making it my own! class Juice: def __init__(self, name, capacity): self.name = name self.capacity = capacity def __add__(self, other): new_name = self.name+"&"+other.name new_capacity = self.capacity + other.capacity return(new_name + (str(" ")) + str("(") + ((str((new_capacity))+"L)"))) 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)
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. cclass 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)())
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)())
# here is my solution. 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', 3.0) print(a.__add__(b)())
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) print(a.name+'&'+b.name+"", '('+str(a.capacity + b.capacity)+'L)')
lass 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)