How to catch StopIteration?

John Salerno johnjsal at gmailNOSPAM.com
Mon Jun 16 23:58:49 EDT 2008


ccy56781 at gmail.com wrote:

> for i, x in enumerate(collatz(13)):
>   try:
>     last = x[:i+1]
>     print x[:i+1]
>   except StopIteration:
>     print last.appnd(1)

My guess would be because StopIteration is raised when control returns 
to the for loop and sees that it has nothing else left. At that point, 
you aren't in the try statement and so the exception can't be caught 
that way. But I should let the experts answer. I need to go to bed 
anyway! :)



More information about the Python-list mailing list