indentation blocking in Python

Chris “Kwpolska” Warrick kwpolska at gmail.com
Sun Oct 27 11:40:38 EDT 2013


On Sun, Oct 27, 2013 at 4:31 PM,  <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:

It’s called the command prompt.
> 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")

You indented the wrong thing.  You put your if/else statement in a
non-standard way (which works, but is discouraged).  Also, you ended
the first line with a semicolon (same case).  So, the proper code
would be:

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

(I resisted the urge to add spaces around `=` and `==`, something most
people want you to do.)

-- 
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense



More information about the Python-list mailing list