indentation blocking in Python

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Oct 27 11:44:59 EDT 2013


On 27/10/2013 15:31, ajetrumpet at gmail.com wrote:
> hello all,
>
> This has got me a tad bit confused I think.  I am running 3.3.0 and I know that Python looks to group code together that is supposed to be in the same block.  But the question is, where are the rules for this?  For instance, if I type the following in a PY file, it errors out and I don't see the DOS window with the output in Vista:
>
> a=1;
>     if a==1: print(1)
>     else: print(0)
> wait = input("press key")
>
> However, if I don't indent anything at all, it works!
>
> a=1;
> if a==1: print(1)
> else: print(0)
> wait = input("press key")
>
> Can someone offer just a little explanation for this?  'IF' and 'ELSE' are obviously in the same code block.  Are they not?  Maybe it's not so obvious.  Thanks.
>

You don't have a new block, the if else is in the same block as a=1, 
which by the way doesn't need that semi colon.  Restructure the if else 
and you must then write.

a=1
if a==1:
     print(1)
else:
     print(0)
wait = input("press key")

HTH.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence




More information about the Python-list mailing list