- 3
Python core juice maker code please
7 Answers
0
show us your code attempt first ^^
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,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)
Not worked
0
you should respect builtin methods name: 'magic' methods always starts and ends with double underscore: init should be renamed to __init__, and so for __str__ and __add__ ^^
0
Read the tip again:
Use the __add__ method to define a custom behavior for the + operator and return the resulting object.
0
Search it in the code section.You will get hundred of example.
0
class Juice:
def __init__(self, name, capacity):
self.name = name
self.capacity = capacity
def __add__(self, other):
return self.name + "&" + other.name + " (" + str(self.capacity + other.capacity) + "L)"
a = Juice('Orange', 1.5)
b = Juice('Apple', 2.0)
result = a + b
print(result)
0
class Juice:
def __init__(self, name, capacity):
self.name = name
self.capacity = capacity
def __add__(self, other):
return self.name + "&" + other.name + " (" + str(self.capacity + other.capacity) + "L)"
a = Juice('Orange', 1.5)
b = Juice('Apple', 2.0)
result = a + b
print(result)