Exit while loop in Python

前端 未结 6 635
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 04:04

    If you don't want to make a function ( which you should and refer to Ashwini's answer in that case), here is an alternate 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
    

提交回复
热议问题