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