I seriously need help now!

The rule is very simple: every time there is a colon : in python code, it is the beginning of a non empty indented block

def foo(): # <- see the colon here ?
    bar()  # <- this line is indented (preferably with 4 spaces)
    baz()  # <- this line too
qux() # <- not indented: this line is outside function foo.


if x == 0: # <- a colon
    if y == 3.14: # <- a colon
    else:  # <- else has the same indentation as the corresponding if
else: # <- a colon
    pass # <- a do-nothing statement to fill the else block
etc() # <- this line is not in a block

Read also this.

Edited 3 Years Ago by Gribouillis

if it is indented, it means it's "With" anotther block of code.
you need to indent evry time there is a colon.If you've seen one already, double indent.

def foo(spam):
    print "bar" #part of the function "foo"
    print 5+4 #also part of "foo"
    if spam:
        print "eggs" #part of if and foo
        if 1+1 == 20:
            print"FAIL" #part of foo,if,if
        elif 1+1 == "2":
            print"FAIL" #part of foo if and elif
        print" done with if" #part of if and foo
    return 5 #part of foo and only foo
foo() #not part of foo because it isn't indented

Edited 3 Years Ago by Gribouillis: oops!

This article has been dead for over six months. Start a new discussion instead.