突破嵌套循环 [英] breaking out of nested loop
问题描述
如果for
循环找到匹配项,那么在while循环中打破这个循环的适当方法是什么?
而1:
$ x $ b 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())
打印停止,停止
休息
else:
继续
除了异常,e:
打印e
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
推荐答案
你需要设置一个带有多个中断的标记标志 - *或*
(可能更pythonic)将它包装在try..except中并引发
例外。定义你自己的异常类,如果你希望避免捕获其他异常,那么只需要陷阱。
没有一个命令可以打破多个循环。
问候,
模糊
http://www.voidspace.org.uk/python
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
rbt写道:
如果for
循环找到匹配项,那么在while循环中打破这个循环的适当方法是什么?
首先定义一个标志:
keepGoing = True
而1:
while keepGoing:
$ x $ b for x in xrange(len(group)):
试试:
....如果匹配==目标:
打印碰撞!!!
print make_string
在这里设置标志,然后休息:
keepGoing = False
break
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
Tada...
-Peter
谢谢伙计......这很棒。现在我理解为什么有时使用逻辑
,例如''而不是真'';
在星期二,2005-07-12 at 10: 51-0400,Peter Hansen写道:
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写道:
如果for
循环找到匹配,什么是打破这个while循环的合适方法?
What is the appropriate way to break out of this while loop if the for
loop finds a match?
首先定义一个标志:
keepGoing = True
Define a flag first:
keepGoing = True
1:
while keepGoing:
for x in xrange(len(group)):
尝试:
for x in xrange(len(group)):
try:
...
...
if match == target:
print" Collision !!!"
print make_string
if match == target:
print "Collision!!!"
print make_string
在此处设置标志,然后执行休息:
keepGoing = False
Set the flag here, then do the break:
keepGoing = False
break
Tada ...
-Peter
Tada...
-Peter
<无线电通信/>
这篇关于突破嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!