9. They're multiplying!


#1



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


In a previous exercise the Animal() instances were working properly with the same syntax as shown. At this exercise the interpreter is not apparently recognizing the class.


It should print the variable health for each animal described as "good".


class Animal(object):
    """Makes cute animals."""
    is_alive = True
    health = "good"
    
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def description(self):
        print self.name
        print self.age
        print hippo.health
        print sloth.health
        print ocelot.health
    description(hippo)
    description(sloth)
    description(sloth)
    hippo = Animal("Potamus",25)
    sloth = Animal("Preguiça",180)
    ocelot = Animal("Jagger",80)

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


#2

We don't want to print instance names inside the method. Use,

self.health

#3

class Animal(object):

is_alive = True
health = "good"

def __init__(self, name, age):
    self.name = name
    self.age = age
def description(self):
    print self.name
    print self.age
    print self.health
description(hippo)
description(sloth)
description(sloth)
hippo = Animal("Potamus",25)
sloth = Animal("Preguiça",180)
ocelot = Animal("Jagger",80)

It shows the error below:

Traceback (most recent call last):
File "python", line 1, in
File "python", line 13, in Animal
NameError: name 'hippo' is not defined

And I tried change position between description() and object declarations:

class Animal(object):

is_alive = True
health = "good"

def __init__(self, name, age):
    self.name = name
    self.age = age
def description(self):
    print self.name
    print self.age
    print self.health
hippo = Animal("Potamus",25)
sloth = Animal("Preguiça",180)
ocelot = Animal("Jagger",80)
description(hippo)
description(sloth)
description(sloth)

And now the error is:

Traceback (most recent call last):
File "python", line 1, in
File "python", line 13, in Animal
NameError: name 'Animal' is not defined

I don't understand what is wrong in any of those.


#4

This is a method of the, so the context is the instance. There is no parameter.

hippo.description()

#5

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