How to jump out of nested 'for'-loops?
D'Arcy J.M. Cain
darcy at druid.net
Mon Sep 28 23:45:43 EDT 2009
On Mon, 28 Sep 2009 22:09:37 -0500
Peng Yu <pengyu.ut at gmail.com> wrote:
> I want some command to jump out of nested loop. I'm wondering what is
> the most convenient way to do so in python.
Forget about jumping out of a loop. Put your loop into a function and
simply return. Much cleaner and clearer.
> for i in range(10):
> print "i = ", i
> for j in range(10):
> if i*10 + j == 50:
> print i*10 + j
> break # I want to jump out of the loops.
def myfunc(li, lim, mul):
for i in li:
print "i = ", i
for j in range(10):
if i*mul + j == lim:
return i*mul + j
print myfunc(range(10), 50, 10)
--
D'Arcy J.M. Cain <darcy at druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
More information about the Python-list
mailing list