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 Python Basics (2015) Logic in Python Having a Conversation

indents in python

Hello,

I am finding myself spending more time trying to figure out how to indent on workspace than learning the code. Can you please tell me the trick to not get stuck on indent errors.

thanks

Hey Alex, I think Sky has moved on, but I enjoyed exploring this with you :D

2 Answers

Just remember these:

Always use four spaces. Don't use four spaces then later use some other amount of indentation.

# Note: this code is just examples it all of these would cause errors in Python

block:
    # Code must be indented four spaces
# Code outside all blocks can't be indented
block:
    # Block in block
    block:
        # Indent extra four spaces

I hope you understand now! ~alex

Hello Alexander,

Thanks a lot for the explanation, but could you please use a real code to explain the green parts. I am still struggling to make sense of when to indent.

thanks!!

Here's an example:

name = "Alex"

def say_hello(name):
    # These if condition are part of the say_hello function so they are indented 4 spaces
    if name == None:
        print("Hello Stranger")
    elif name == "Alex":
        print("Why, hello, Alex!")
    elif name == "Orange Sky":
        print("Nice name!")
    else:
        print("Huh? Who are you?")

# This code isn't part of the say_hello function so it is outside of both the say_hello
# function and the "if" conditions
say_hello(name)

Hello Alexander,

In the around and around video, the for num in [1,2,3,4]: is not indented but the if num%2 == 0: us indented . I thought an if clause is also a block, why is the if clause indented with 4 spaces?

thanks

Can you provide a link to the video and also the time you saw that part (for example 2 minutes and 30 seconds into the video)?

Thanks ~alex

for num in [1,2,3,4]: # <-- if this is the start of this bit of programming, it would not be indented
    if num % 2 == 0:  # <-- this if is indented because it is acting on the previous line 
        print(num)  # <-- if there were a print, it would be indented



# if the whole thing were in a function, that whole block of code would be indented. 
# Maybe, (I haven't thought this # through)  every time there is a :, the next line is indented.

You're right!

If the previous line has a colon at the end of the line, the next line of code is indented (unless it's a comment) an extra 4 spaces (or 1 tab)

Example:

def indent_example():
    for x in range(100):
        if x == 64:
            print("My favorite number!")
            for y in range(4):
                if y == 1:
                    print("Cool!")
                else:
                    print("Epic!")

And, in fact, if you want to try this program, you can run it for yourself :)

I hope you understand now.

~Alex