Exiting a loop in Python

In the code below, I would like the while complete as soon as a + b + c = 1000 . However, testing with print statements shows that it continues until for loops are executed. I tried while True , and then in the if set False if , but this leads to an infinite loop. I thought using x = 0 , and then setting x = 1 might work, but that also works until the for loops end. What is the smartest and fastest way out? Thanks.

 a = 3 b = 4 c = 5 x = 0 while x != 1: for a in range(3,500): for b in range(a+1,500): c = (a**2 + b**2)**0.5 if a + b + c == 1000: print a, b, c print a*b*c x = 1 
+6
source share
6 answers

The while will meet the condition only when the control returns to it, i.e. when the for loops are complete. So, why your program did not exit immediately, even if the condition was met.

But in case the condition was not met for any values โ€‹โ€‹of a , b , c , then your code will end in an infinite loop.

You should use a function here, since the return will do what you ask for.

 def func(a,b,c): for a in range(3,500): for b in range(a+1,500): c = (a**2 + b**2)**0.5 if a + b + c == 1000: print a, b, c print a*b*c return # causes your function to exit, and return a value to caller func(3,4,5) 

Besides @Sukrit Kalra answer , where he used exit flags, you can also use sys.exit() if your program does not have any code after this code block.

 import sys a = 3 b = 4 c = 5 for a in range(3,500): for b in range(a+1,500): c = (a**2 + b**2)**0.5 if a + b + c == 1000: print a, b, c print a*b*c sys.exit() #stops the script 

help sys.exit :

 >>> print sys.exit.__doc__ exit([status]) Exit the interpreter by raising SystemExit(status). If the status is omitted or None, it defaults to zero (ie, success). If the status is numeric, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (ie, failure). 
+6
source

If you don't want to do any function (which you should and should be attributed to Ashwini's answer in this case), here is an alternative implementation.

 >>> x = True >>> for a in range(3,500): for b in range(a+1, 500): c = (a**2 + b**2)**0.5 if a + b + c == 1000: print a, b, c print a*b*c x = False break if x == False: break 200 375 425.0 31875000.0 
+3
source

You can reorganize the internal code into a function and use return to exit:

 def inner(): for a in range(3,500): for b in range(a+1,500): c = (a**2 + b**2)**0.5 if a + b + c == 1000: print a, b, c print a*b*c return False return True while inner(): pass 

Check out this question.

+1
source

The problem is that you set x = 1 when a + b + c == 1000, you do not exit two loops for loops when this condition is met, and therefore the while loop does not know that x == 1 before those until both ends of the cycle end. To avoid this problem, you can add explicit break statements to for loops (and, as Sukrit Kalra points out, a while loop becomes unnecessary).

 a = 3 b = 4 c = 5 x = 0 for a in range(3,500): for b in range(a+1,500): c = (a**2 + b**2)**0.5 if a + b + c == 1000: print a, b, c print a*b*c x = 1 break if x==1: break 
+1
source

You can wrap try/excep and raise when the condition is met.

 class FinitoException(Exception): pass a = 3 b = 4 c = 5 x = 0 try: for a in range(3,500): for b in range(a+1,500): c = (a**2 + b**2)**0.5 if a + b + c == 1000: print a, b, c print a*b*c raise FinitoException() except FinitoException: return # or whatever 
0
source

You can use the break statement:

 a = 3 b = 4 c = 5 x = 0 while x != 1: for a in range(3,500): for b in range(a+1,500): c = (a**2 + b**2)**0.5 if a + b + c == 1000: print a, b, c print a*b*c break 
0
source

Source: https://habr.com/ru/post/945449/


All Articles