2

I have a function called evaluate and it is a function that takes time to complete. It then resumes a loop, skipping that iteration:

My code:

array = []#some huge array containing different ways of expressing greetings

def main(resumeLocation):
    for a in range(len(array)):
        i = array[a]
        if a < resumeLocation:
            continue
        else:
            if (i == "Hello")
                answer(i, a)
                break

def answer(input, resumeLocation):
    # process answer
    resumeLoop(resumeLocation)

now, in order for the function not to loop infinitely, I need to skip the iteration where I process the answer, so I need to break the loop, call that function, then resume the loop, however, I cannot seem to work out how to do this.

any suggestions are helpful.

Thank you

3
  • Why do you want to break from the loop? Commented Mar 10, 2018 at 3:46
  • because, if I don't then that function will be called and an infinite loop will happen Commented Mar 10, 2018 at 3:47
  • 2
    docs.python.org/3/library/functions.html#enumerate Commented Mar 10, 2018 at 3:47

1 Answer 1

1

As some of the comments have mentioned, I believe there are better ways to do this, but if you really want to start a loop back where you broke out of it, you can try this approach:

arr = [1,2,3,4,5]

def answer(x, y, arr):
  print('About to restart the loop at index: ', x+1)
  loop(x+1, arr)

def loop(i, arr):
  for x in range(i, len(arr)):
    t = arr[x]
    print(t)
    if t == 3:
      answer(x, t, arr)
      break

loop(0, arr)

When the condition is met, answer() is called and the loop is broken, however the current index is preserved, and then when answer completes, the function is called with the correct starting index. The output of this code is as follows:

1
2
3
About to restart the loop at index:  3
4
5

It correctly restarts the loop at the next index.

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.