stuck in python OOP | SoloLearn: Learn to code for FREE!

+1

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)

1/19/2021 9:28:11 AM

kaspars kaspars

16 Answers

New Answer

+5

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!

+4

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

+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)

+3

kaspars kaspars If this is still unclear to you, then Please feel free to ask. Thanks!

+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__".

+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:((

+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)"

+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 --> )

+2

kaspars kaspars I would highly recommend the use of f-strings and forget about using concatenation in the form mentioned. f-strings are so much more flexible and tend to be more readable? Consider your return line: return f'{self.name} & {other.name} ({self.capacity + other.capacity}L)' Happy Coding! 😎👍

+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)

+2

kaspars kaspars It should have quotation marks and f-prefix f"{self.name}&{other.name}" >> Apple&Orange

+1

And here is an example of how add method works. https://www.sololearn.com/learn/Python/2470/

+1

kaspars kaspars take a look at this code and visit the links in the comments. It should help explain it in more depth. https://code.sololearn.com/c0hUDfOLt1kx If you don't want to see the evolution from formatting strings to f-strings, just go to this link: http://zetcode.com/python/fstring/ Happy Coding! 😎👍

+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)'

+1

kaspars kaspars read the link I posted above and it explains it. There are other option other than f' that you may run into also. Read the documentation, this is just the f-string syntax and it will always be different based on your variables and how you want to use them. The f' is just a prefix telling the interpreter it is a formatted string, just like r' tells it is a raw string or u' designates a unicode string... there are others. The link above explains it with examples to follow.

+1

Official docs, if you really want to dig into it: https://www.python.org/dev/peps/pep-0498/#