They're Multiplying- Intro to Classes Clarification


#1



https://www.codecademy.com/en/courses/python-intermediate-en-WL8e4/1/4?curriculum_id=4f89dab3d788890003000096#

Hi All, the code doesn't really misbehave, I would like a clarification on the Hint.

The hint suggests I should print hippo.health at the end. but my code works just fine. Have I misunderstood the exercise or is this just another way to do it ? please let me know.

I just added health to the description function, assuming this would make things easier to call at the end.


class Animal(object):
    """Makes cute animals."""
    is_alive = True
    health = "good"
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def description(self):
        print self.name
        print self.age
        print self.health
hippo = Animal("John Snow", 25)
sloth = Animal("John Cena", 16)
ocelot = Animal("Jason Statham", 53)
print hippo.description()
print sloth.description()
print ocelot.description()


#2

I believe by printing out the description of hippo that includes its health, you did what they wanted and therefore to conclude this might be another method to do the excersise. Although I would prefer to use hippo.health because it is much simpler and easier. That is because I am not sure if the code would have worked if you did not define health in description in the first place.
Happy Coding


#3

The way your code is written the health of the hippo is included in the instructions. Had you only added the health variable to line 4 as the instructions lay out, you would have had to call print hippo.health separately in order to print that particular line. So the hint is more for people who don't put 2 & 2 together to add the print line to the description() function.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.