+ 6

Hello, can anyone tell me what is wrong with this code please

I need to complete the code to allow the addition of two Zumo objects, resulting in a new Zumo object with the combination of capacity names of the two juices that have been added. For example, if you add an orange juice with a capacity of 1.0 and an apple juice with a capacity of 2.5, the resulting juice should have: name: Orange & Apple capacity: 3.5 The result basically is this: Orange&Apple (3.5L). I made this code class Juice: def __init__(self, name, capasity): self.name = name self.capasity = capasity def __str__(self): return (self.name + ' ('+str(self.capacity)+'L)') x = Juice("Orange",32.0) y = Juice("Apple",23.5) z = x.name+"&"+y.name e = x.capasity + y.capasity print (z,e) and the result is: Orange&Apple 3.5 But i need the other result: Orange&Apple (3.5L) For some reason I doesn't work my code in order to get the "(3.5L)" part. If anyone could help me with this it would be great

13th Jan 2022, 4:06 AM
Alan Restrepo
Alan Restrepo - avatar
32 Answers
+ 10
anthony silver, you need __add__() magic method, it needs self, other inside brackets, and because __init__ has name, capasity as arguments, also in __add__() method you need both of them, when you return your function with + operation. (Don't forget "&" between self.name and other.name) And; at the end you only need print(x + y) nothing more. Hope it helps
13th Jan 2022, 7:52 AM
SoloilSole
SoloilSole - avatar
+ 3
Cristian Gabriel Mazzulla espera tienes razón , lo que me hacía falta era poner en def __add__(self) otro argumento aparte de self así : def __add__(self,x) y ya, tenías razón era algo sencillo
13th Jan 2022, 5:01 AM
Alan Restrepo
Alan Restrepo - avatar
+ 1
Pay attention to the hint: "Use the __add__ method to define a custom behavior for the + operator and return the resulting object." Review Lesson 75.1! •Do not copy and paste the answer, if you do not understand it, there is no point in taking any course.
13th Jan 2022, 4:29 AM
CGM
CGM - avatar
+ 1
I already understand what you are doing wrong, you are printing the variables separately! You must print a new object that is the sum of both
13th Jan 2022, 4:48 AM
CGM
CGM - avatar
+ 1
Cristian Gabriel Mazzulla ok gracias por la ayuda, lo aprecio
13th Jan 2022, 5:58 AM
Alan Restrepo
Alan Restrepo - avatar
0
Dang! One solution is change the last line to :-> print(z+'('+e+')')
14th Jan 2022, 9:50 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
0
Apart from grammatical errors like misspelling capacity your also made quotation errors like where you typed return(self.name + '('+str(self.capacity)
14th Jan 2022, 9:03 PM
oladiipo ayanlekan ayomide
- 1
First, the spelling capacity capasity I don't know how to describe it. The z and e are different objects from Juice, and they will call other __str__ Cristian Gabriel Mazzulla is right. Review will be helpful.
13th Jan 2022, 4:14 AM
FanYu
FanYu - avatar
- 1
𝓕𝓛𝓨 thanks for the correction, but still doesn't give me the result ( Orange& Apple (3.5L))
13th Jan 2022, 4:17 AM
Alan Restrepo
Alan Restrepo - avatar
- 1
Cristian Gabriel Mazzulla exactly for the fact that i want to learn , is why I'm asking for help , because I'm not sure why is not working the code, maybe is something simple but I'm not getting it
13th Jan 2022, 4:40 AM
Alan Restrepo
Alan Restrepo - avatar
- 1
Cristian Gabriel Mazzulla even though I changed the __str__ magic method for the __add__ method it still give me the result: Orange&Apple 3.5
13th Jan 2022, 4:44 AM
Alan Restrepo
Alan Restrepo - avatar
- 1
Cristian Gabriel Mazzulla You speak Spanish?
13th Jan 2022, 4:49 AM
Alan Restrepo
Alan Restrepo - avatar
- 1
To add them correctly is that you must define __add__, so you can simply use it "x + y" and get the result And yes, hablo español, y el inglés es díficil para mí, se nota? jaja
13th Jan 2022, 4:50 AM
CGM
CGM - avatar
- 1
Cristian Gabriel Mazzulla no, de hecho te entiendo que eso es lo importante, ya puse el método __add__ pero me da un error cuando hago que se imprima el resultado
13th Jan 2022, 4:54 AM
Alan Restrepo
Alan Restrepo - avatar
- 1
Bien, puedes compartir tu método __add__? Lo que debe hacer es sencillo, debe retornar la suma de ambos objetos como un nuevo objeto, lo que tú hiciste en z y e como los valores de un nuevo objeto
13th Jan 2022, 4:58 AM
CGM
CGM - avatar
- 1
Genial! Lo hiciste!🥳 jaja
13th Jan 2022, 5:03 AM
CGM
CGM - avatar
- 1
Cristian Gabriel Mazzulla lol pero sabes que, no me había fijado pero solo me sale una parte así: Orange ( 32.0L) no todo completo, ahora no sé cómo hacer para que salga todo completo así: Orange &Apple (55.5)
13th Jan 2022, 5:06 AM
Alan Restrepo
Alan Restrepo - avatar
- 1
No entiendo, te refieres a cuando haces la suma y concatenas los nombres? Puedes usar el método de strings join, que toma como parámetro una lista de strings, así: "&".join([self.name, x.name]) No sé si te refieres a eso..
13th Jan 2022, 5:14 AM
CGM
CGM - avatar
- 1
Cristian Gabriel Mazzulla mira lo que pasa es lo siguiente el código se ve asi class Juice: def __init__(self, name, capasity): self.name = name self.capasity = capasity def __add__(self,x): return (self.name + ' ('+str(self.capasity)+'L)') x = Juice("Orange",32.0) y = Juice("Apple",23.5) result = x+y print (result) Y cuando le pongo para el resultado me sale así: Orange (32.0L) Y no el resto, nose si me explico bien
13th Jan 2022, 5:19 AM
Alan Restrepo
Alan Restrepo - avatar
- 1
Cristian Gabriel Mazzulla gracias por la ayuda, es bueno encontrar a alguien que hable español
13th Jan 2022, 5:23 AM
Alan Restrepo
Alan Restrepo - avatar