breaking out of nested loop

2005-07-12 Thread 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 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread Fuzzyman
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread Peter Hansen
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread rbt
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread Duncan Booth
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 ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread Steven D'Aprano
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.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread Jeremy Sanders
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/
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: breaking out of nested loop

2005-07-12 Thread Tim Golden
[Jeremy Sanders]
| 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...



Presumably you meant "cue discussion..."



(Ducks & runs)
TJG


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread Andrew Koenig
"rbt" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> 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.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread Terry Hancock
On Tuesday 12 July 2005 10:28 am, Tim Golden wrote:
> [Jeremy Sanders]
> | 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...
> 
> 
> 
> Presumably you meant "cue discussion..."
> 
> 

Or "queue" as in, it's going to have to take a number and wait for
someone to be bored enough with the ten other language "enhancement"
threads that have gone through lately.  ;-)

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-13 Thread Raymond Hettinger
[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

-- 
http://mail.python.org/mailman/listinfo/python-list