- 3

Python core juice maker code please

23rd Feb 2021, 4:07 PM
mayank upadhyay
mayank upadhyay - avatar
7 Respostas
0
show us your code attempt first ^^
23rd Feb 2021, 4:30 PM
visph
visph - 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,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
23rd Feb 2021, 4:33 PM
mayank upadhyay
mayank upadhyay - avatar
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__ ^^
23rd Feb 2021, 4:38 PM
visph
visph - avatar
0
Read the tip again: Use the __add__ method to define a custom behavior for the + operator and return the resulting object.
23rd Feb 2021, 4:40 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Search it in the code section.You will get hundred of example.
23rd Feb 2021, 5:33 PM
Sonam Tashi
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)
24th Apr 2021, 3:54 PM
Faramarz Kowsari
Faramarz Kowsari - avatar
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)
30th Jun 2021, 2:49 AM
M Prashanth Rao
M Prashanth Rao - avatar