This forum is now read-only. Please use our new forums at discuss.codecademy.com.

1054 points
5168e70f720111040f002066_65523160
Submitted by
Vinícius Camargo
about 6 years ago

Indentation Error [solved]

Does someone know why this error keep appearing?

Traceback (most recent call last):
File "runner.py", line 125, in compilecode
File "python", line 3
health = "good"
^
IndentationError: unexpected inden

Here is my code:

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, self.age

hippo = Animal('vini', 21)
sloth = Animal('joaquim', 20)
ocelot = Animal('clementina', 22)

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

I can't figure out where is the error.


9 votes

permalink

Hi i had the same error. I just copied your the code below and it worked for me.
Thanks.

694 points
92f68d7a9047969c2a8b4cdca3ff6323?s=140&d=retro
Submitted by
yossarianlives
almost 6 years ago

4 Comments

51bcb6347c82cabe9a00f582_726395366 Yuheng Cai almost 6 years ago

I did the same with you.it worked well,but still don't know what was wrong.

352f8bc086b85cb6a7e2f5aed2f83aa1?s=140&d=retro Tj Martinez almost 6 years ago

I tested and your code above is good, it passed no problem (yes: copy/paste)

52a0675252f863edb300197d_692065897 RSG almost 5 years ago

nvm i figured it out it was an indentation error on the last print and 3 things above it...

52a0675252f863edb300197d_692065897 RSG almost 5 years ago

thanks for your efforts


2 votes

permalink

I have the solution to the problem
You should only redo all the tabs in the code yourself
then the problem is solved

Because Python use tab to recognize the structure of the code, so the error occurs when the tab is not correct

1325 points
C508c381c34d2e80a24296f11539a85b?s=140&d=retro
Submitted by
Edwardsj
almost 6 years ago

2 Comments

F2150abbbedad5a77e079b5798f846bc?s=140&d=retro suzuran almost 6 years ago

It works for me, thank you!

C842fb8a727e1d859bb79ddabca912a1?s=140&d=retro amstree_51c1f1057c82caf7f9002b48_del almost 6 years ago

thx!!


1 vote

permalink

I had this, probably spaces in there instead of tabs.

781 points
Ccfc3260ad14f26021c871dbb3eeb8eb?s=140&d=retro
Submitted by
Aaron
almost 6 years ago

1 Comment

5168e70f720111040f002066_65523160 Vinícius Camargo almost 6 years ago

thanks, it worked!


1 vote

permalink

Banged my head on this for a while. Deleting every tab in a code (everything aligned to the left) and putting 'em back worked for me...

801 points
87d78354b312b00af1de1f95e079a230?s=140&d=retro
Submitted by
Roman P.
almost 6 years ago

1 Comment

99f2fe24ece11a42648480816aca6eb5?s=140&d=retro that1chrisguy almost 6 years ago

This worked for me also.


1 vote

permalink

I think this needs to be fixed. I know the difference between spaces and tabs matters but the editor in Codecademy makes these indentations for us and then tells us the indentation is wrong. It's through no fault of our own.

1044 points
D5ccc15ce12fd80ef62e98e04a86ca91?s=140&d=retro
Submitted by
micjett
almost 6 years ago


0 votes

permalink

My tab length grew one space shorter, not sure why. But 4 mechanical spaces worked for me. Glad I could move on.

892 points
51914aac6c5e7477f8000f80_496902547
Submitted by
Mallikarjuna S Kaveti
almost 6 years ago


0 votes

permalink

I kept getting indent errors no matter what I did - deleting and reformatting white space was unsuccessful - until I added a space after init like so:

def __init__ (self, name, age):

422 points
519ea114d65098935300182b_415579584
Submitted by
Paul Cerra
almost 6 years ago


0 votes

permalink

cheers after re-indenting code it passes. I'm really struggling to get my head around classes so thought my code was off, after several re-writes and going back to original was a relief to find it wasn't me but the pre-written bit that was off

1115 points
Picture
Submitted by
james
almost 6 years ago


-1 votes

permalink

I tried your code on my system with Python 3.3.2 and it gave following error:
$ python test.py
File "test.py", line 8
print self.name, self.age
^
SyntaxError: invalid syntax

414 points
Picture
Submitted by
Aalok Kamble
almost 6 years ago


-2 votes

permalink

simple thing is put a {tab} before the health = "good"

557 points
51868def8f4060c0b30000a5_605830785
Submitted by
Jc
almost 6 years ago