0
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.

2
  • maybe use continue? Commented Feb 9, 2018 at 3:36
  • What are you trying to accomplish? Commented Feb 9, 2018 at 4:06

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, just remember to mark this as answered if it helped solve your problem!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.