0

stuck in python OOP

hello, so i have tried to learn programming for years and i have time to learn.24/7 now but. i just dont get it how. tolearn, i dont understand nothing but If Else etc.., have finished courses like this some 3-4x already . Im trying again and i cannot. figure out how to fix this. juice maker in OOP for hours!. :( Juice Maker You are given a Juice class, which has name and capacity properties. You need to complete the code to enable and adding of two Juice objects, resulting in a new Juice object with the combined capacity and names of the two juices being added. For example, if you add an Orange juice with 1.0 capacity and an Apple juice with 2.5 capacity, the resulting juice should have: name: Orange&Apple capacity: 3.5 The names have been combined using an & symbol. Use the __add__ method to define a custom behavior for the + operator and return the resulting object. class Juice: def __init__(self, name, capacity): self.name = name self.capacity = capacity def __str__(self): return (self.name + ' ('+str(self.capacity)+'L)') a = Juice('Orange', 1.5) b = Juice('Apple', 2.0) result = a + b print(result) i tried something like this but it is wrong! def __add__(self): return ((self.a + "&" + self.b) +capacity)

19th Jan 2021, 9:28 AM
kaspars kaspars
kaspars kaspars - avatar
17 Respuestas
+ 8
You are adding two objects of a class. So you may want to add a magic method, or dunder that will be executed when you add two objects. def __add__(self, other) : self --> represents the first object other --> represents the second object It is up to you how you will complete this function and solve the problem. If you have more questions, feel free to ask. Thanks and Good Luck!
19th Jan 2021, 9:33 AM
noteve
noteve - avatar
+ 5
Your code is almost correct, the capacity just have to be converted into string: def __add__(self, other): return self.name + "&" + other.name + " (" + str(self.capacity + other.capacity) + "L)" Remember that the return value of __add__ will be assigned to "result" variable and we will print that returned value. - - - NAME CONCATENATE- - - So here, we first concatenate two strings which are the two fruits/juice. For example: ----> self.name + "&" + other.name # will result to ----> Apple&Juice - - - - -CAPACITY- - - Now whats missing is the capacity in Liters So we add there capacity together and then we convert the result into String. For example: ---> " (" + str(self.capacity + other.capacity) + "L)" # will result to ---> (3.5L) - - - - - - -CONCATENATE NAME AND CAPACITY- - - - - - ---> self.name + "&" + other.name + " (" + str(self.capacity + other.capacity) + "L)" # will result to ---> Apple&Juice (3.5L) Final Code: https://code.sololearn.com/cPLkan162qaw/?ref=app
19th Jan 2021, 11:28 AM
noteve
noteve - avatar
+ 3
Thanks nicko12. I got this far def __add__(self,other): return (self.name + "&" + other.name) this makes the result go - Apple&Orange but again i tried variations like this and still cant figure out def __add__(self,other): return ((self.name + "&" + other.name) + self.capacity + other.capacity)
19th Jan 2021, 9:49 AM
kaspars kaspars
kaspars kaspars - avatar
+ 3
kaspars kaspars If this is still unclear to you, then Please feel free to ask. Thanks!
19th Jan 2021, 11:36 AM
noteve
noteve - avatar
+ 2
kaspars kaspars Hint: We cannot concatenate or add strings and numbers. To convert floats/integers into string ---> str(number) And by the way, I think the dunder __str__ is no longer needed as we are already using the variable "result" which has the returned value of "__add__".
19th Jan 2021, 10:20 AM
noteve
noteve - avatar
+ 2
abhai link dont work and damn i just cant figure it out what and where i need to add to the code, have no idea:((
19th Jan 2021, 11:20 AM
kaspars kaspars
kaspars kaspars - avatar
+ 2
thanks for the ansert so I just removed the def __STR__ and added just like you showed " (" + str(self.capacity + other.capacity) + "L)" to the same lane. i was. close but i wrote. it wrong. the thing isi cant understand why this L is + "L)" and not "..... + "L" ) " these. "" is really confusing in this lanes,could someone explain what and why these "" look so confusing here?i cant understand what each "" close and open. " (" + str(self.capacity + other.capacity) + "L)"
19th Jan 2021, 11:53 AM
kaspars kaspars
kaspars kaspars - avatar
+ 2
kaspars kaspars Thanks for asking. Yes these two are the same: ---> string + "L)" # and ---> string + "L" + ")" Because the capacity will look like this (3.5L) Parts: --> ( --> 3.5 --> L --> )
19th Jan 2021, 11:56 AM
noteve
noteve - avatar
+ 2
kode krasher, iwrite this code and it do not work! but when i copy your it work! return {self.name}&{other.name} + ({self.capacity}+{other.capacity}L)
19th Jan 2021, 2:39 PM
kaspars kaspars
kaspars kaspars - avatar
+ 2
kaspars kaspars It should have quotation marks and f-prefix f"{self.name}&{other.name}" >> Apple&Orange
19th Jan 2021, 2:41 PM
noteve
noteve - avatar
+ 1
19th Jan 2021, 9:36 AM
Abhay
Abhay - avatar
+ 1
what does that f' do? i allways need to p ut f" before {name}? and it does not work if i write it my selfand not copy paste.. i put f' but it give me error return f'{self.name}&{other.name} + ({self.capacity}+{other.capacity}L)'
19th Jan 2021, 4:01 PM
kaspars kaspars
kaspars kaspars - 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)
24th Apr 2021, 3:54 PM
Faramarz Kowsari
Faramarz Kowsari - avatar