How to restart a loop on exception

Viewed 1030

I have this code:

for i in range(n):
    for v in range (m): 
        # code
        try:
            # more code
        except IndexError:
            os.system('python "Example.py"')

os.system('python "Example.py"') allows me to reinitialize the entire code, but what I really want to do is to go back right before the for v in range(m): part and start that loop again. How can I do this? Is there any other way to do that instead of adding a function? If not, how can I code that function?

4 Answers
for i in range(n):
    v=0
    while (v < m): 
        # code
        try:
            # more code
        except IndexError:
            v-=1
        v+=1

You can do this:

restart = True

for i in range(n):

    while restart:

        for v in range (m): 
            # code
            try:
                # more code
                restart = False
            except IndexError:
                # more code

you can try this:

for i in range(n):
    getting_errors = True
    while x:
        for v in range(m):
            try:
                #code
            except Exception:
                #code
                break
            else:
                getting_errors = False

Slightly neater than Gzegzor's example.

for i in range(n):
    v=0
    while (v < m): 
        # code
        try:
            # more code
        except IndexError:
            continue
        v += 1
Related