breaking out of nested loop

Peter Hansen peter at engcorp.com
Tue Jul 12 10:51:45 EDT 2005


rbt wrote:
> What is the appropriate way to break out of this while loop if the for
> loop finds a match?

Define a flag first:

keepGoing = True

> while 1:
while keepGoing:

>     for x in xrange(len(group)):
>         try:        
...
>             if match == target:
>                 print "Collision!!!"
>                 print make_string

Set the flag here, then do the break:
                   keepGoing = False

>                 break

Tada...

-Peter



More information about the Python-list mailing list