[Tutor] One of my 'baby step' programs

John Fouhy john at fouhy.net
Mon Oct 2 06:58:36 CEST 2006


On 02/10/06, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> I had no idea you could have an 'else' tied to a 'while' loop.
> Interesting....

It allows you to distinguish between exiting the loop via a break and
exiting the loop normally.

eg:

>>> for i in range(10):
...  break
... else:
...  print 'foo'
...
>>> for i in range(10):
...  pass
... else:
...  print 'foo'
...
foo
>>>

There was a discussion of for..else linked in Dr Dobb's Python URL
recently: http://groups.google.com/group/comp.lang.python/browse_thread/thread/bc1d8038e65c449/

-- 
John.


More information about the Tutor mailing list