This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Vinícius Camargo
almost 11 years

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.

Answer 51b30d96631fe93a120069bc

9 votes

Permalink

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

points
Submitted by yossarianlives
almost 11 years

4 comments

Yuheng Cai almost 11 years

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

Tj Martinez almost 11 years

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

RSG almost 10 years

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

RSG almost 10 years

thanks for your efforts

Answer 51b74d8c631fe9756900b1c2

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

points
Submitted by Edwardsj
almost 11 years

2 comments

suzuran almost 11 years

It works for me, thank you!

Answer 519b977ede46e4a03d003f84

1 vote

Permalink

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

points
Submitted by Aaron
almost 11 years

1 comments

Vinícius Camargo almost 11 years

thanks, it worked!

Answer 51a4dc8e271ead54fc000987

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…

points
Submitted by Roman P.
almost 11 years

1 comments

that1chrisguy over 10 years

This worked for me also.

Answer 51a685b7393885f8b90026f5

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.

points
Submitted by micjett
almost 11 years

Answer 51a70e1f9a526d0840004967

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.

points
almost 11 years

Answer 51afd12e282ae3b20b000134

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):
points
Submitted by Paul Cerra
almost 11 years

Answer 51b8d0348c1ccced3b00069e

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

points
Submitted by james
almost 11 years

Answer 51c979e452f8632110001b11

-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

points
Submitted by Aalok Kamble
almost 11 years

Answer 519f25060f4f899df1003844

-2 votes

Permalink

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

points
Submitted by Jc
almost 11 years