Python – `break` out of all loops
Question:
I am using multiple nested for
loops. In the last loop there is an if
statement. When evaluated to True
all the for
loops should stop, but that does not happen. It only break
s out of the innermost for
loop, and than it keeps on going. I need all loops to come to a stop if the break
statement is encountered.
My code:
for i in range(1, 1001):
for i2 in range(i, 1001):
for i3 in range(i2, 1001):
if i*i + i2*i2 == i3*i3 and i + i2 + i3 == 1000:
print i*i2*i3
break
Answers:
You should put your loops inside a function and then return:
def myfunc():
for i in range(1, 1001):
for i2 in range(i, 1001):
for i3 in range(i2, 1001):
if i*i + i2*i2 == i3*i3 and i + i2 + i3 == 1000:
print i*i2*i3
return # Exit the function (and stop all of the loops)
myfunc() # Call the function
Using return
immediately exits the enclosing function. In the process, all of the loops will be stopped.
You can raise an exception
try:
for a in range(5):
for b in range(5):
if a==b==3:
raise StopIteration
print b
except StopIteration: pass
Not sure if this the cleanest way possible to do it but you could do a bool check at the top of every loop.
do_loop = True
for i in range(1, 1001):
if not do_loop:
break
for i2 in range(i, 1001):
# [Loop code here]
# set do_loop to False to break parent loops
why not use a generator expression:
def my_iterator()
for i in range(1, 1001):
for i2 in range(i, 1001):
for i3 in range(i2, 1001):
yield i,i2,i3
for i,i2,i3 in my_iterator():
if i*i + i2*i2 == i3*i3 and i + i2 + i3 == 1000:
print i*i2*i3
break
I am using multiple nested for
loops. In the last loop there is an if
statement. When evaluated to True
all the for
loops should stop, but that does not happen. It only break
s out of the innermost for
loop, and than it keeps on going. I need all loops to come to a stop if the break
statement is encountered.
My code:
for i in range(1, 1001):
for i2 in range(i, 1001):
for i3 in range(i2, 1001):
if i*i + i2*i2 == i3*i3 and i + i2 + i3 == 1000:
print i*i2*i3
break
You should put your loops inside a function and then return:
def myfunc():
for i in range(1, 1001):
for i2 in range(i, 1001):
for i3 in range(i2, 1001):
if i*i + i2*i2 == i3*i3 and i + i2 + i3 == 1000:
print i*i2*i3
return # Exit the function (and stop all of the loops)
myfunc() # Call the function
Using return
immediately exits the enclosing function. In the process, all of the loops will be stopped.
You can raise an exception
try:
for a in range(5):
for b in range(5):
if a==b==3:
raise StopIteration
print b
except StopIteration: pass
Not sure if this the cleanest way possible to do it but you could do a bool check at the top of every loop.
do_loop = True
for i in range(1, 1001):
if not do_loop:
break
for i2 in range(i, 1001):
# [Loop code here]
# set do_loop to False to break parent loops
why not use a generator expression:
def my_iterator()
for i in range(1, 1001):
for i2 in range(i, 1001):
for i3 in range(i2, 1001):
yield i,i2,i3
for i,i2,i3 in my_iterator():
if i*i + i2*i2 == i3*i3 and i + i2 + i3 == 1000:
print i*i2*i3
break