python class definition -- name undefined or unexpected indent -


class animal(object):          """makes cute animals."""         is_alive = true         def __init__(self, name, age):         self.name = name         self.age = age     # add method here!     def description(self):         print self.name         print self.age  hippo = animal("steve",100)  hippo.description()  

--code piece one

the error got:

traceback (most recent call last):   file "runner.py", line 125, in compilecode   file "python", line 3     is_alive = true    ^ indentationerror: unexpected indent 

no idea of happening

can please tell me wrong? lot!

it looks you're mixing tabs , spaces. means you see indentation, , interpreter sees, different. lead hard-to-notice indentationerrors.

in particular:

        """makes cute animals."""         is_alive = true         def __init__(self, name, age):         self.name = name 

the first 2 lines have 8 spaces. third has 7 spaces , tab. i'm not sure whether python treats more indented 8 spaces—which error, because there's no reason indent here—or refuses guess 1 counts more indentation other. either way, it's wrong. , next line has 2 tabs.

to fix code, delete tabs, , re-indent spaces.

the simple way avoid in future never use tabs in code.

you may want consider using better editor, insert spaces, if hit tab key. or, failing that, @ least , editor can show tabs, can spot problems when arise.

finally, whenever indentationerrors when code looks fine, try running code -t flag (python -t myscript.py instead of python myscript.py) check whether reason.

as karl knechtel points out in comment, pep 8 (the python style guide) has a section on this.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -