8. A Methodical Approach - I don't know what's wrong


#1

This is my code:

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def descripion():
        return self.name
        return self.age
        
hippo = Animal("Hipster", 6)

print hippo.is_alive, hippo.description

I think it should work but it says:

:warning:Oops, try again. It seems like your code raises an error, see the console window for the error message!

And the console says:

True Traceback (most recent call last):
File "python", line 14, in
AttributeError: 'Animal' object has no attribute 'description'

I don't know what's wrong. I kept switching it from print to return but return didn't make a difference.


#2

@corecoder78572,

In Python the indentation is very important
and an error in this indentation will generate exactly the kind error you are encountering.

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

==================================================


#3

Thank you, @leonhard_wettengmx_n! I didn't completely know how that worked, but I realized that the problem was a spelling error. Instead of typing description:

def description():
        return self.name
        return self.age

I typed descripion:

    def descripion():
        return self.name
        return self.age

So-

print hippo.is_alive, hippo.description

-didn't work.


#4

This is not valid python code, your first return will be executed but the second will fail to run.

Valid:

def description(self):
    return self.name, self.age

This is valid python code.


#5

I kept switching it from print to return but return didn't make a difference, but I had the returns wrong syntax. Thanks @zeziba!


#7

Really struggled on this one, working code below, as top code doesn't work, sorry not a master of indentation in the posts

`class Animal(object):
"""Makes cute animals."""
is_alive = True
def init(self, name, age):
self.name = name
self.age = age
# Add your method here!
def description(self):
print self.name
print self.age

hippo = Animal("Hipster", 6)

print hippo.is_alive, hippo.description


#8

This worked for me:

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def description(self):
        print self.name 
        print self.age
        
hippo = Animal("Hipster", "6")

hippo.description()

#9

print hippo.is_alive, hippo.description()


#10

K this worked for me:

class Animal(object):
"""Makes cute animals."""
is_alive = True
def init(self, name, age):
self.name = name
self.age = age
# Add your method here!

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

hippo = Animal("Bob", 17)

print hippo.description()

My indenting was off. Since you are trying to add a method to the Animal class, when you define the description method, it needs to be indented correctly underneath


#11

@livinspree, @codeace96610, @dutchmofo2014,
IMHO as you are allready using the print statement in your description-Method
you could call the Method like

hippo.description()

#12

I got a pass for the following code:

But it printed out this:

What's going on here?


#13

You are printing hipp.description() inside the description,while the instructions says to print outside.


#14

class Animal(object):
"""Makes cute animals."""
is_alive = True
def init(self, name, age):
self.name = name
self.age = age
# Add your method here!
def description(self):
print self.name
print self.age
hippo= Animal("Borya", 18)
hippo.description()


#15

Can anyone explain please why both indentation give us a pass???

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    def __init__(self, name, age,):
        self.name = name
        self.age = age
    def description(self):
        print self.name
        print self.age
    hippo = Animal("John", 5)
    hippo.description()

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    def __init__(self, name, age,):
        self.name = name
        self.age = age
    def description(self):
        print self.name
        print self.age
hippo = Animal("John", 5)
hippo.description()

#16

Hi, @orouge ,

This is an interesting case.

Version 2 is correct, and that is why it passes when you submit it.

Version 1 is incorrect. If you refresh the page and then submit version 1, you will get an error, because it incorporates these two statements into the definition of the Animal class ...

hippo = Animal("John", 5)
hippo.description()

The first of those two statements will attempt to instantiate an Animal before the definition of the Animal class has been completed, and this message will appear in the console window ...

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

However, if you submit version 1 without first refreshing the page, right after you have submitted version 2, version 1 will pass, because Codecademy will still have the completed Animal class definition from version 2 in memory when it encounters the instantiation statement.


#17

Thanks @appylpye.

Are we creating following model by calling function at the end? if it was it would be "Animal(object)" tho. right? since description is not indented as def description(self) and communicates to Animal(object) i get confused about "how to read it" again as it is not following model obviously

def y():
   code


y()

#18

The pattern we are following in your second version of class Animal is similar, in some respects, to what you have done above with the function, y, but there are some noteworthy differences.

You have defined y as a global function, and you have defined description as a method that belongs to class Animal. In both cases, you called them, later, from a global scope. But in the case of class Animal, you created an instance of it prior to calling the description method. The action of the description method is meaningful only in the context of its being called in relation to an instance of the Animal class, since its purpose is to output information that pertains specifically to an Animal instance.


#19

guys, this code works:

class Animal(object):
"""Makes cute animals."""
is_alive = True
def init(self, name, age):
self.name = name
self.age = age
# Add your method here!
def description(self):
print self.name
print self.age
hippo=Animal("jose","12")
hippo.description()


#20

My Code:
class Animal(object):
"""Crea animales lindos."""
esta_vivo = True
def init(self, nombre, edad):
self.nombre = nombre
self.edad = edad
# ¡Agregá acá tu método!
def descripcion(self):
print self.nombre
print self.edad
hipopotamo = Animal('Pepe', '8')
hipopotamo.descripcion()

RESULTS:
Pepe
8
None

ERROR MESSAGE:

¡Uy! Probá otra vez. Parece que tu método 'descripcion' no muestra la descripcion de tu hipopótamo en la consola.

I don't understand what is wrong...


#23