Exit while loop in Python

前端 未结 6 633
Happy的楠姐
Happy的楠姐 2020-12-06 03:37

In the code below, I\'d like the while loop to exit as soon as a + b + c = 1000. However, testing with

6条回答
  •  悲哀的现实
    2020-12-06 03:46

    You could use a 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
    

提交回复
热议问题