Getting error while printing member variable


#1


<
getting this error, please help
Traceback (most recent call last):
File "python", line 1, in
File "python", line 20, in Animal
AttributeError: 'Animal' object has no attribute 'health'
.>


class Animal(object):
    """Makes cute animals."""
    health = "good"
    is_alive = True
    
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    # Add your method here!
    
    sloth=Animal("Slothy",2)
    ocelot=Animal("Oceloty",2)
    hippo=Animal("Hippy",2)
    def description(obj):
        print obj.name,
        print obj.age
        
        
    print hippo.health
    print sloth.health
    print ocelot.health


#2

Hi @umorise ,

These instances of Animal should be created outside the definition of the class Animal rather than within ...

    sloth=Animal("Slothy",2)
    ocelot=Animal("Oceloty",2)
    hippo=Animal("Hippy",2)

Also place these print statements outside the class Animal definition ...

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

No comma is needed here ...

print obj.name,

The output of the name and age attributes from the description method should be on two separate lines.


#3

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