在Python中将嵌套for循环转换为while循环

时间:2016-09-30 17:26:17

标签: python loops

我是python的初学者,我使用了我从eclipse中知道的4嵌套for循环。我想将我的循环压缩成while循环,但我不确定如何这样做。有人可以帮帮我吗?这是我的代码:

import sys
n = int(sys.argv[1])

# step 1 four nested loops
for a in range(1, n + 1):
          a3 = a*a*a
          if a3 > n:
                    break
          for b in range(a + 1, n + 1):
                    b3 = b*b*b
                    if a3 + b3 > n:
                              break
                    for c in range(a + 1, n + 1):
                              c3 = c*c*c
                              if c3 > a3 + b3:
                                        break
                              for d in range(c + 1, n + 1):
                                        d3 = d*d*d
                                        if c3 + d3 > a3 + b3:
                                                  break
                                           if a3 + b3 == c3 + d3:                         
                                             print str(a3 + b3), " = ", a, "^3", " + ", b, "^3", " = ", c, "^3", " + ", d, "^3"

0 个答案:

没有答案