Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Indentation Error

I keep getting told about an indentation error. As far as I know this series isn't explicit about how to use indentations. It's quite frustrating. I'm left to guess and try and keep getting told the same error.

1 Answer

Hi Gary. In python, indentation is used to start and end code blocks (by this i mean for loops, while loops, conditional statements etc). When you start a block of code, you use indentation to signify that this code belongs to this block. To end a code block, you dedent. Usually, you would indent by 4 spaces or you can just press the tab key. Any lines that are indented at the same level belong to that block of code. An example of this would be:

for i in range(10):
    print("Hello") #this code is indented and therefore belongs to the for loop.
                   #this will happen 10 times

If you wish to exit this block of code, you go back to writing with no indentation:

for i in range(10):
    print('Hello') #this code is indented and therefore belongs to the for loop.
print('bye') #this will only be printed once as it is not part of the block of code above

The same principal goes for defining functions, conditional statements, loops etc.