[Tutor] trouble with indents
alan.gauld at btinternet.com
Mon May 28 10:36:31 CEST 2007
"adam urbas" <adamurbas at hotmail.com> wrote in
> I'm not sure when to indent. I understand that it has to be done.
Ok, But you need to understand *why* it has to be done.
To do that you need to understand the basic constructs
of programming: sequences, branches, loops and modules.
(these are described in the concepts section of my tutorial)
Basically each construct is defined by indenting in python.
Thus if you have a loop in your code the set of instructions
to be repeated is indented. This makes it visually clear what
gets repeated. More importantly it tells the Python interpreter
what it should repeat!
Thus, simplistically, you need to indent anything following
That is after a branch instruction: if/elif/else
or a loop: for/while
or a function(module) definition: def
or inside a class: class
The next trick is to determine when to stop indenting and
that's actually harder to describe! Its basically when you
want Python to stop treating your code as special - eg no
longer part of the code to be repeated in a loop.
> I need somewhere to start from the beginning.
There are several non-programmers tutorials
(including mine :-). Try one of them.
Dive Into python is an excellent book for experienced
programmers or after you have gone through one of those
Author of the Learn to Program web site
More information about the Tutor