Introduction to Classes Lesson 9 What's wrong?


#1

class Animal(object):
"""Makes cute animals."""
is_alive = True
health = "good"

def __init__(self, name, age):
    self.name = name
    self.age = age

def description(object):        
    print object.name 
    print object.age

hippo = Animal("Fernando", 19) 
sloth = Animal("animal", 23)
ocelot = Animal("anima", 45)

description(hippo)

print hippo.health
print sloth.health
print ocelot.health

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


#2

@checkmort,
=indentation= is VERY important in Python....!!!!

class Animal(object):
    #indentation 4 spaces ==> start of Animal class definiton
    """Makes cute animals."""
    is_alive = True
    health = "good"
    def __init__(self, name, age):
        #extra 4 spaces for the __init__ code-block
        self.name = name
        self.age = age

    def description(object):        
        #extra 4 spaces for the description() code-block
        print object.name 
        print object.age
#indentation 0 ==> END-of-Animal class defintion
#creating an Instance of the Animal class
hippo = Animal("Fernando", 19) 
sloth = Animal("animal", 23)
ocelot = Animal("anima", 45)

#description is a method available to every Instance of Animal
hippo.description()

print hippo.health
print sloth.health
print ocelot.health