How do I know they want a list?

I don’t understand anything about this code. What is it doing? What does " Give Atom a .__add__(self, other) method that returns a Molecule with the two Atom s." even mean? How do I test this code to see what each step is doing?

An __add__ method within a class is a method that can be called on an instance of the class just by using the + operator. You can define to do anything that you would like the + operator to do within your class.

class MiddleSchool:
    def __init__(self, name):
        self.name = name
    def __add__(self, other):
        return "{} loves {}, ha ha!!!".format(self.name, other)    
    
paul = MiddleSchool("Paul")
print(paul + "Maria")

# Output:
Paul loves Maria, ha ha!!!
2 Likes

Thanks Patrick!
I am really confused with the question itself. " Give Atom a .__add__(self, other) method that returns a Molecule with the two Atom s.". After seeing the answer I get that they want a Molecule object returned with a list of the two Atom objects in a list. Is it common in programmer “lingo” to say return “x” with “number” of “y” and have that mean to return the ""y"s in a list?

No; the assignment could have been phrased more clearly. They could have expected a string or a tuple, for all you knew. The point to remember is that in your class, you can make + do anything you want!

For a challenge, you could code it so that ‘carbon’ + ‘oxygen’ + ‘oxygen’ returns ‘CO2’.

Thank you! I would give you a tip if i could!
:smile:

1 Like

Yeh this lesson is totally not the best one. And excersice is super weird. At first I didn’t even understand what exactly they want us to make. My first solution was more like @theworldcup.

@Codecademy edit this lesson please :slight_smile: Make it more understandable.

7 Likes

Mine was this, hahaha:

return Molecule()+ self.label*2

As you can see CC doesn’t care, they just hope that other people in this forum will do their job for them for free. As you can see, may people have problems with it, they KNOW about it but almost a years passed and…nothing.