0

Many times happened to me that I was coding 3 or 4 nested loops, the problem is that with the break statement, I could only skip one of the loops and the rest of those would continue to proceed...

Is there anyway to break all of the nested loops?

ex:

a = 3
b = 4
c = 5
while a <= 333:
    b = a + 1
    while  b <= 500:
        c = 1000 - a - b
        while c < 500:
            if c**2 == (a**2) + (b**2) and a + b + c = 1000:
                print("this is the first number : ", a)
                print("this is the second number : " ,b)
                print("and this is the third number : " ,c)
                break
            else :
                c +=1
        b +=1  
    a +=1
    print(a)

It's the code I have written for Project Euler #9!

https://projecteuler.net/problem=9

when the condition of if statement is met... how can I stop those while loops from running?

Kian Maghsoodi
  • 123
  • 1
  • 7

2 Answers2

3

try create a function, and use return statement.

Gustavo Rossi Muller
  • 1,062
  • 14
  • 18
0

There isn't a way to break out of nested loops with one break statement in python.

KoolAid
  • 299
  • 1
  • 4
  • 13