Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57745 > unrolled thread

indentation blocking in Python

Started byajetrumpet@gmail.com
First post2013-10-27 08:31 -0700
Last post2013-10-27 16:40 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  indentation blocking in Python ajetrumpet@gmail.com - 2013-10-27 08:31 -0700
    Re: indentation blocking in Python "Colin J. Williams" <cjw@ncf.ca> - 2013-10-27 11:37 -0400
    Re: indentation blocking in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-27 15:44 +0000
    Re: indentation blocking in Python Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-10-27 16:40 +0100

#57745 — indentation blocking in Python

Fromajetrumpet@gmail.com
Date2013-10-27 08:31 -0700
Subjectindentation blocking in Python
Message-ID<96fc985d-725e-4fe7-8d30-0bcaad91f975@googlegroups.com>
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.

[toc] | [next] | [standalone]


#57747

From"Colin J. Williams" <cjw@ncf.ca>
Date2013-10-27 11:37 -0400
Message-ID<526D3336.4090508@ncf.ca>
In reply to#57745
On 27/10/2013 11:31 AM, ajetrumpet@gmail.com wrote:
> a=1;
> if a==1: print(1)
> else: print(0)
> wait = input("press key")
You indent only subordinate statements.

You don't need a semi-colon unless it separates two statements on the 
same line.

Your code:

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

I hope that this helps.

Colin W.

[toc] | [prev] | [next] | [standalone]


#57748

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-27 15:44 +0000
Message-ID<mailman.1651.1382888712.18130.python-list@python.org>
In reply to#57745
On 27/10/2013 15:31, ajetrumpet@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

[toc] | [prev] | [next] | [standalone]


#57750

FromChris “Kwpolska” Warrick <kwpolska@gmail.com>
Date2013-10-27 16:40 +0100
Message-ID<mailman.1652.1382890138.18130.python-list@python.org>
In reply to#57745
On Sun, Oct 27, 2013 at 4:31 PM,  <ajetrumpet@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web