简体   繁体   中英

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. I was wondering if i could use some sort of for loop to shorten this. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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