简体   繁体   English

Python-for循环中有多个while循环?

[英]Python - Multiple while loops in a for loop?

while z[0] <= D(0):
  if z[1] >= z[0]:
    if Fractionate(1) > 0:

      while z[1] <= D(1):
        if z[2] >= z[1]:
          if Fractionate(2) > 0:

            while z[2] <= D(2):
              if z[3] >= z[2]:
                if Fractionate(3) > 0:

Here I have a bunch of while loops and they're all doing the same thing. 在这里,我有一堆while循环,它们都在做同样的事情。 I was wondering if i could use some sort of for loop to shorten this. 我想知道是否可以使用某种for循环来缩短此时间。 I can't just do 我不能只是做

for i in range(0, k - 2):
  while z[0] <= D(0):
    if z[i+1] >= z[i]:
      if Fractionate(i+1) > 0:

Because it only does 1 while loop at a time. 因为它一次只执行1个while循环。

I don't know exactly what you're trying to do, but I would recommend using a recursive function, something like this: 我不知道您要做什么,但是我建议使用递归函数,如下所示:

def recurse(n):
    while z[n] <= D(n):
        if z[n+1] >= z[n]:
            if Fractionate(n+1) > 0:
                return recurse(n+1)
            else:
                return base value

recurse(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM