4 Spaces are Important

One of the most important things to remember is that spaces at the beginning of a line are important.

The first line of a program must not have any spaces at the beginning of the line.

In the blinky.mpy example the first line

pindir(P1_6, OUT)

Has no spaces in front of it. But further down the lines of code that follow the while 1: line do have spaces. This is because the while 1: command starts a new block of code.

Everytime a new block of code is started the number of spaces at the beginning of the line increases.  It is common to use 4 spaces for every new block.

The indentation is the number of space characters at the beginning of a line.  Commands or statements which go together are called a block,  and all the lines in a block must have the same indentation.

For example the following code is correct:

pindir(P1_6, OUT)
while 1:
   out(P1_6,1)
   wait(500)
   out(P1_6,0)
   wait(500)

But the following is incorrect:

pindir(P1_6, OUT)
while 1:   
   out(P1_6,1)
     wait(500)
   out(P1_6,0)
   wait(500)

This is because the third line has a different number of spaces at the beginning of the line than the other lines in the block.  This is an error, and the mpyEditor will give a indentation error if you make this type of mistake. An indentation error is a very common type of error.

(Note: Be careful if you use a different editor than the mpyEditor for your programs. Some editors will insert an ascii Tab character instead of the 4 spaces, and this can cause an indentation error (even if it looks lined up). It can be difficult to see this type of error. You can see the spaces in the mpyEditor by setting it with View->Editor->Show Whitespace .)