The use of :

bruno modulix onurb at xiludom.gro
Sun Nov 28 13:45:08 EST 2004


Timo Virkkala a écrit :
> Chang LI wrote:
> 
>> Paul Robson <autismuk at autismuk.muralichucks.freeserve.co.uk> wrote in 
>> message 
>> news:<pan.2004.11.26.08.33.40.219098 at autismuk.muralichucks.freeserve.co.uk>... 
>>
>> So the : is similar to "begin" 
> 
> 
> Correct

Not correct. You need both the ':' and the extra level of indentation on 
next line, ie:

Python 2.3.3 (#2, Feb 17 2004, 11:45:40)
 >>> while True:
... print "aha"
   File "<stdin>", line 2
     print "aha"
         ^
IndentationError: expected an indented block
 >>>


>> and the last space line is similar to "end", right?
> 
> 
> Nope. You don't need a space line (you mean an empty line by that, 
> right?), you can just outdent one level and continue without any empty 
> lines (although in the interactive interpreter you need the empty line 
> to end the block on the first indentation level). So you could do:
> 
> i = 5
> while i > 0:
>     print i
>         i = i-1
> print "That's it"

<meta> Is this a pb when typing of copying ? It's just plain wonrg code 
anyway :
 >>> while i > 0:
...     print i
...             i -= 1
   File "<stdin>", line 3
     i = i-1
     ^
SyntaxError: invalid syntax
 >>> while i > 0:
...     print i
...     i -= 1
...
5
4
3
2
1
 >>>

The level of indentation has to be the same within a block, and it has 
to be *1* (*one*) extra-or-less level of indentation than the previous 
block.

<please-someone-correct-me-if-i-am-wrong>
In fact, from a purely technical POV, the ':' could have been omitted 
from the Python syntax, since indentation does the whole job of defining 
blocks. It's only here for readability AFAIK.
</please-someone-correct-me-if-i-am-wrong>

Bruno



More information about the Python-list mailing list