0
classmethod: i want to add two fruit juice name and their capacity in litre to out out , name& name(5.0L) using add method in py
Pls can some one help me out thanks
13 Respostas
+ 3
Please post your attempt, so we can see where your problem lies (or describe your problem more detailed)
+ 1
class method:
def __init__(self,name,capacity):
self.name = name
self.capacity =capacity
def __add__(self,other):
name = self.name + "&" + other.name
capacity = self.capacity +other.capacity
return (name, capacity)
a = Juice('Orange',1.5)
b = Juice('Apple', 2.0)
result = a + b
print(result)
Expected output is Orange&Apple(3.5L)
My output is ( 'Orange&Apple' 3.5)
Thanks
+ 1
Instead of returning (name,capacity) you could/should create a new object like:
Juice(name,capacity)
And implement the __str(self)__ method (not sure whether thats the correct method name, but sonething like this)
+ 1
I will give it a try
+ 1
monsuru omotoso instead of pasting the code in answers, pls edit your question and include a link to your code in Code Playground. It is far more readable, allows testing, debugging and solving, and avoid too much pollution.
Also, your code seems incomplete. Did you create a class called "method" but instantiate from another one called "Juice"?
+ 1
Alexander Thiem good to instruct the OP to post his/her attempt. Just add the instructions to link from Code Playground instead of pasting walls of code.
+ 1
Thank for all your suggestion and correction. I appreciate everyone
+ 1
Thank you all i have gotten solution to the code
0
This is my attempt
0
Emerson Prado He only posted parts of the code⊠he posted 2 class-methods
0
Finished?
0
Alexander Thiem Yes. That's a problem too - how can one analyze incomplete code?
0
Emerson Prado well it was enough for me thoughâŠ