Home > other >  How to restart a for loop
How to restart a for loop

Time:09-05

I have a piece of code. Here, I am running a for loop. If the if statement is not met, I want to restart that for loop. How should I do this? sp is a library btw.

for i in range (10000):
     #my codes

     a= sp.levene(#my variables)
     
     if  a[1] < 0.05:
       #I want to restart for loop again 

     else:

        #doing something

CodePudding user response:

You probably don't want to use a for loop, since you aren't iterating over a particular sequence of numbers (i is going to jump around based on what happens inside the loop). Using a while you'd do:

i = 0:
while i < 10000:
     # my code
     a = sp.levene()  # my variables
     
     if  a[1] < 0.05:
        i = 0
        continue
     i  = 1

     # doing something

continue restarts the loop at the beginning of the loop body, and having set i = 0 it's now in the same state it was in at the first iteration.

CodePudding user response:

The simplest way to handle this is:

while True:
    for i in range(100000):
         ...
         if a[1] < 0.05:
            # this will exit out of the for loop, but the while
            # loop will keep going
            break
         else:
            ....
    # if we've successfully finished the "for" loop, then break out of
    # the while loop
    break

If your logic is a little bit more complicated:

done = False
while not done:
    for i in range(100000):
         ...
         if a[1] < 0.05:
            # this will exit out of the for loop, but the while
            # loop will keep going
            break
         else:
            # set done to True if you've decided you don't need to perform
            # the outer loop any more
            other stuff

      
    # likewise, set done to True if you're done with the outer while loop

CodePudding user response:

To build on Frank Yellin's answer if you don't want to use break else break.

continueloop=True
while(continueloop):
    for i in range (10000):
        #my codes
        a=sp.levene #my variables
        if a[1] < 0.05:
            #I want to restart for loop again 
            continueloop=True
        else:
            continueloop=False
            #doing something

Hope you find a suitable answer!

CodePudding user response:

I think what you are looking to do is have that function inside a loop. If the statement fails then on the else statement call the function again with new parameters. Basically, you want recursion on your loop is what I'm understanding.


def RecursionFunc()  #3) when this function is called code runs from here
for i in range (10000):
     #my codes

     a= sp.levene(#my variables)
     
     if  a[1] < 0.05:

       RecursionFunc() #2) will make you jump to the top again basically calling itself
       break     #4) will exit the current loop
     else:

        
RecursionFunc() # 1)This will be the first code that gets executed

And the recursion will keep the function going and you can do plenty of other stuff with this. I know you want to break the loop and run again you can also change the "i" value on the next recursion run. If you give recursionFunc(int i) then you can basically set yours for loop to a new I value on the next run too. Can do a lot of cool things like this.

  • Related