Breaking out of nested loop

Gillou glenfant at equod.com.nospam
Tue Feb 27 18:07:47 CET 2001


Not very sexy but it should work...

import exceptions
class LoopBreaker(exceptions.Exception):
    def __init__(self):
        pass
...
try:
    while 1:
        while 1:
        ...
        raise LoopBreaker
...
except LoopBreaker:
    pass
...

"Dan" <dwndgg at opti.cgi.net> a écrit dans le message news:
3a9b44f1.226505016 at news.okc1.ok.home.com...
>
> How might one go about breaking out of a multi level loop?
>
> For instance, the following code will go into an endless loop.
>
> while(1):
>     while(1):
>         break
>
>
> Is there a statement that one can use to break out of multiple levels
> of loop control?  I really hate to start littering my code with flag
> variables and if statements.
>
> -Dan
>




More information about the Python-list mailing list