How to know which lines of code to indent?



<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
Nothing. I just wanted to know which lines of code is it required to indent. Why didn’t I have to on line 1 and 5?
<What do you expect to happen instead?>
Not sure.


Replace this line with your code.
def spam():
eggs = 12
return eggs

print spam()

<do not remove the three backticks above>


Anything that would be included in the code block is indented.

def foo_function (params):   # signature line (header) no indent
    # code block, one level of indent
    if ____:
        # code block, two levels of indent
        for ___:
            # code block, three levels of indent
    return    # return line, one level of indent (typical)


What do you mean by the “two levels of indent” “three levels of indent” and then “typical”.
I thought you either do two spaces or four every time. Also, is the code block considered everything except the “signature line”.


Yes. The signature line is always in outer scope, the code block in inner scope. Outer scope can access the function, but not the variables.

It gets a little hazier with non-functions. Scope is the general concern, though, and indentation is how we tell the interpreter what our intended scope is. Block scope is important in loops and conditionals. If the concept of scope is still a gray area, then take this opportunity to pause and do some research. It is very important that this not be a stumbling block moving forward. It will be if you do not understand scope.

It refers to nesting… When an object is (or group of objects are) contained in another object. A function is an object, and all its code is contained in a nested code block. Indentation is our way of passing that information to the interpreter.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.