pythonfor-loopwhile-loopnested-loops

Problem with nested loop logic and indentation in python


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            

Solution

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