3

I am trying to re-run a nested for loop within a while loop if the while condition is not met. If the condition is met then I want the inner loop to terminate and proceed to the next iteration of the outer loop. I want to try the inner loop 10 times before preceding to the next i of the outer loop. I've tried various variations of the following (psuedocode) but am unsure exactly what the if / else statements at the end the function should say, break/continue? And where they should be indented to accomplish this. I've appended the indentation lengths to each part of the code. I would be grateful if anyone can help explain if it's possible to do this and how to do it. Thanks in advance.

def function(input):   

counter1 = 0    

for i in range(0, 10): #indent5  - outer loop
   #assign some stuff to variables based on value of i...
    counter2 = 0
    var1 = cats[i]
    var2 = dogs[i]
    x = len(input)
   
    MAX_RETRIES = 10
    attempts = 0
   
    while attempts < MAX_RETRIES and counter2 < x: #indent9 
   
        for var in var1 : #13  - inner loop
            if blah == blah: #indent17
               #do a lot of stuff here with cats and dogs...
               random.choice(etc, etc)
                #nested ifs
                if blahblah == blahblah: #indent21
                      counter1 += 1
                if blahblahblah == blahblahblah: #indent21
                      counter2 += 1
                        
        if at end of inner loop counter2 == x:  #indent where?
            #go to next iteration of OUTER loop
            break / continue ? #?
            
        else: #indent where?
            #repeat INNER loop - how?
            break / continue ?
            #reduce counter1 by value of counter2
            counter1 -= counter2
            #reset counter2 to 0 to start inner loop again
            counter2 = 0
              
return blah            
5
  • 1
    "re-run a nested for loop within a while loop if the while condition is not met" - this makes no sense to me. can you reword the description to explain what you are trying to do, and maybe why, so it's more understandable? try to explain the logic you're trying to implement without talking about the implementation (loops and nested loops and etc.) Commented Aug 10 at 15:49
  • if you want to exit while-loop then you need break Commented Aug 10 at 16:05
  • sometimes code may need the same if in two places - ie. inside for-loop to exit it before its end, and outside for-loop to exits while-loop and skip rest of code inside while-loop Commented Aug 10 at 16:07
  • Sorry if my explanation is a bit unclear here. I am trying to figure out how to repeat the inner for loop until the condition counter2 == x is met. It's pretty impossible to explain this without talking about loops! I want to retry this up to MAX_RETRIES. If its met then go to next iteration of outer loop where the line "for var in var2" will refer to different variables. The detail here concerning cats and dogs is just illustrative. What my actual code is doing is trying to pair up elements in 2 separate lists based on multiple criteria. For each successful pairing it adds 1 to counter2 Commented Aug 10 at 16:22
  • The R language has a repeat statement which will re-run a block of code until a certain condition is met. I am basically trying to accomplish the same thing in python. Commented Aug 10 at 16:26

1 Answer 1

2

I don't know what your code is doing but sometimes code may need the same if in two places - to exist inner for-loop and to exit outer while-loop (and skip rest of code)

    while attempts < MAX_RETRIES and counter2 < x: #indent9 
   
        for var in var1 : #13  - inner loop
            if blah == blah: #indent17
                #do a lot of stuff here with cats and dogs...
                random.choice(etc, etc)
                #nested ifs
                if blahblah == blahblah: #indent21
                    counter1 += 1
                if blahblahblah == blahblahblah: #indent21
                    counter2 += 1
                    # exit `for`-loop
                    if counter2 == x:
                        break
                 
        # exit `while`-loop       
        if counter2 == x:
            break
            
        #reduce counter1 by value of counter2
        counter1 -= counter2
        #reset counter2 to 0 to start inner loop again
        counter2 = 0

It can be also created with one break and with inverted condition in second if
because while ... counter2 < x will exit it when counter2 == x

    while attempts < MAX_RETRIES and counter2 < x: #indent9 
   
        for var in var1 : #13  - inner loop
            if blah == blah: #indent17
                #do a lot of stuff here with cats and dogs...
                random.choice(etc, etc)
                #nested ifs
                if blahblah == blahblah: #indent21
                    counter1 += 1
                if blahblahblah == blahblahblah: #indent21
                    counter2 += 1
                    # exit `for`-loop
                    if counter2 == x:
                        break
                 
        # inverted condition
        if counter2 != x:           
            #reduce counter1 by value of counter2
            counter1 -= counter2
            #reset counter2 to 0 to start inner loop again
            counter2 = 0

If this part of code would be in separated function then it could use single return inside if blahblahblah == blahblahblah: instead of two break in two places.
But it would need to send many parameters to function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.