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

17th Jun 2022, 6:03 PM
monsuru omotoso
13 Respostas
+ 3
Please post your attempt, so we can see where your problem lies (or describe your problem more detailed)
17th Jun 2022, 6:06 PM
Alexander Thiem
Alexander Thiem - avatar
+ 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
17th Jun 2022, 6:26 PM
monsuru omotoso
+ 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)
17th Jun 2022, 6:32 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
I will give it a try
17th Jun 2022, 6:46 PM
monsuru omotoso
+ 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"?
18th Jun 2022, 1:18 AM
Emerson Prado
Emerson Prado - avatar
+ 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.
18th Jun 2022, 1:19 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Thank for all your suggestion and correction. I appreciate everyone
18th Jun 2022, 8:15 AM
monsuru omotoso
+ 1
Thank you all i have gotten solution to the code
18th Jun 2022, 1:25 PM
monsuru omotoso
0
This is my attempt
17th Jun 2022, 6:27 PM
monsuru omotoso
0
Emerson Prado He only posted parts of the code
 he posted 2 class-methods
18th Jun 2022, 6:49 AM
Alexander Thiem
Alexander Thiem - avatar
0
Finished?
18th Jun 2022, 9:38 AM
Alexander Thiem
Alexander Thiem - avatar
0
Alexander Thiem Yes. That's a problem too - how can one analyze incomplete code?
18th Jun 2022, 1:22 PM
Emerson Prado
Emerson Prado - avatar
0
Emerson Prado well it was enough for me though

18th Jun 2022, 1:24 PM
Alexander Thiem
Alexander Thiem - avatar