Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > breaking out of nested loop

Reply
Thread Tools

breaking out of nested loop

 
 
rbt
Guest
Posts: n/a
 
      07-12-2005
What is the appropriate way to break out of this while loop if the for
loop finds a match?

while 1:
for x in xrange(len(group)):
try:
mix = random.sample(group, x)
make_string = ''.join(mix)
n = md5.new(make_string)
match = n.hexdigest()
if match == target:
print "Collision!!!"
print make_string
Stop = time.strftime("%H:%M:%S-%m-%d-%y", time.localtime())
print "Stop", Stop
break
else:
continue
except Exception, e:
print e
 
Reply With Quote
 
 
 
 
Fuzzyman
Guest
Posts: n/a
 
      07-12-2005
You either need to set a marker flag with multiple breaks - *or*
(probably more pythonic) wrap it in a try..except and raise an
exception. Define your own exception class and just trap for that if
you want to avoid catching other exceptions.

There is no single command to break out of multiple loops.

Regards,

Fuzzy
http://www.voidspace.org.uk/python

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      07-12-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
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      07-12-2005
Thanks guys... that works great. Now I understand why sometimes logic
such as 'while not true' is used

On Tue, 2005-07-12 at 10:51 -0400, Peter Hansen wrote:
> 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


 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      07-12-2005
rbt wrote:

> What is the appropriate way to break out of this while loop if the for
> loop finds a match?
>
> while 1:
> for x in xrange(len(group)):


another option not yet suggested is simply to collapse the two loops into a
single loop:

import itertools

for x in itertools.cycle(range(len(group)):
... as before ...
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-12-2005
On Tue, 12 Jul 2005 10:19:04 -0400, rbt wrote:

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


Refactor it into something easier to comprehend?

And comments never go astray.


(Untested. And my docstrings are obviously bogus.)

def make_one_thing(group, x):
"""Makes a thing by plonking the frobber.
Expects group to be a list of foo and x to be an index.
"""
mix = random.sample(group, x)
make_string = ''.join(mix)
n = md5.new(make_string)
match = n.hexdigest()
return match

def group_matches(group, target):
"""Cycles over a group of foos, plonking the frobber of each
item in turn, and stopping when one equals target.
"""
for x in xrange(len(group)):
try:
match = make_one_thing(group, x)
if match == target:
return True
except Exception, e:
# don't stop just because the program has a bug
print e
# if we get here, there was no successful match after the
# entire for loop
return False

def test_until_success:
"""Loop forever, or until success, whichever comes first.
"""
group = [1, 2, 3, 4]
target = 5
flag = False
while not flag:
print "No matches yet, starting to search..."
flag = group_matches(group, target)
# if we ever get here, it means we found a collision, and
# flag became True, so the while loop just dropped out
print "Collision!!!"
stop = time.strftime("%H:%M:%S-%m-%d-%y", time.localtime())
print "Stopped at", stop



--
Steven.


 
Reply With Quote
 
Jeremy Sanders
Guest
Posts: n/a
 
      07-12-2005
rbt wrote:

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


queue discussion why Python doesn't have a "break N" statement...

--
Jeremy Sanders
http://www.jeremysanders.net/
 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      07-12-2005
"rbt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


Make it a function and use a "return" statement to break out.


 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      07-12-2005
On Tue, 12 Jul 2005 10:19:04 -0400, rbt <(E-Mail Removed)>
declaimed the following in comp.lang.python:

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


Uh... test for a match ON the while? (Your indentation is also
incorrect).

NoMatch = True #<<<<<<<<<<
while NoMatch: #<<<<<<<<<<
for x in xrange(len(group)):
try:
mix = random.sample(group, x)
make_string = ''.join(mix)
n = md5.new(make_string)
match = n.hexdigest()
if match == target:
NoMatch = False #<<<<<<<<<<<
Stop = time.strftime("%H:%M:%S-%m-%d-%y",
time.localtime())
print "Collisiion"
print make_string
print Stop
break
except Exception, e:
print e

--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      07-13-2005
[rbt]
> What is the appropriate way to break out of this while loop if the for
> loop finds a match?
>
> while 1:
> for x in xrange(len(group)):
> try:
> mix = random.sample(group, x)
> make_string = ''.join(mix)
> n = md5.new(make_string)
> match = n.hexdigest()
> if match == target:
> print "Collision!!!"
> print make_string
> Stop = time.strftime("%H:%M:%S-%m-%d-%y", time.localtime())
> print "Stop", Stop
> break
> else:
> continue
> except Exception, e:
> print e


I would wrap the whole thing in a function definition. When you find a
match, just return from the function. Besides cleanly exiting from
multiple loops, the function approach usually leads to better factoring
(in this case, segregating the search logic from everything else).


Raymond

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
breaking out of multiple nested loops Tagore C Programming 42 12-05-2009 03:23 PM
RE: breaking out of nested loop Tim Golden Python 1 07-13-2005 05:59 AM
Looking for a breaking news rss feed that really contains breaking news Amy XML 0 02-22-2005 06:31 PM
Breaking out of nested subroutine? Hemant Shah Perl Misc 30 08-07-2004 03:01 AM



Advertisments