43 Respostas
+ 55
You 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 š
+ 68
#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)())
+ 40
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)
+ 11
Thanks Elis Sala
+ 8
Hacker Badshah thanks for helpš
+ 6
why is my result = a + b wrong
+ 4
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)
+ 4
My result a + b has a error
+ 2
I have a question how can i put texte where i can write after run the programme in phyton
+ 1
var = input("your text goes here")
+ 1
Apple and orange juice aim only 90% right
+ 1
Def __add__(self,n):
Self.name+="&"+str(n.name)
Self.capacity+=n.capacity
Return self.__str__
Print(a.__ad__(b)()
+ 1
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)
+ 1
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)())
+ 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,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)())
+ 1
# 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)())
+ 1
The simplest solution
a = ("Orange")
b = ('Apple')
print (a+'&'+b+ ' (3.5L)' )
+ 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)
print(a.name+'&'+b.name+"", '('+str(a.capacity + b.capacity)+'L)')
+ 1
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)