no SyntaxError?

Moshe Zadka moshez at math.huji.ac.il
Wed Jun 14 01:41:41 EDT 2000


On Tue, 13 Jun 2000, Robert W. Bill wrote:

> Just curious- I didn't find any references to this in a quick search of 
> the faq and elsewhere...
> 
> This snippet generates "SyntaxError: invalid syntax"
> 
> def test():
>     else: print "whoops"
> test()
> 
> However, the following snippet generates "0 1 whoops" when it seems like
> it should also generate "SyntaxError: invalid syntax" because there is no 
> if before the else. 
>  
> def test1():
>     for i in range(2):
>         print i,
>     else:
>         print "whoops"

for/else is just as valid as if/else (and so are while/else).
Read more about it in the tutorial.
--
Moshe Zadka <moshez at math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com





More information about the Python-list mailing list