Newbie question: Explain this behavior

George Sakkis gsakkis at rutgers.edu
Thu Jul 14 20:30:38 EDT 2005


"David Smith" <ac063 at lafn.org> wrote in message news:db6tfq$1kbh$1 at zook.lafn.org...
> Why does code snippet one work correctly, but not two.  The only
> difference is the placement of the "else".  I know that indentation
> affects execution, but how does it change behavior in the following
> examples?  Thank you.
>
> [snipped]

In the second example, "else" corresponds to "if", while in the first one it corresponds to "for".
The tutorial example on "for ... else" loops
(http://docs.python.org/tut/node6.html#SECTION006400000000000000000) uses the prime number
computation you posted, so read it again if it's not clear what the "else" clause does with loops.

George





More information about the Python-list mailing list