2

Is there a way to make python jump back to the beginning of your code. Say for example I have a program that contains 3 while loops. I run the programs and get through all the while loops.

Is there a way I can then make the program jump back to line 1 of the code, and start all over again? I know the "continue" statement will take you to beginning of the while loop it is in. Was curious to know if there is another statement or something similar that can take you to beginning of your code from anywhere in your program?

Any help would be greatly appreciated. Spent time googling and searching stack overflow, but everytime I found an instance of this question, it was never really specifically answered, just better code was suggested.

I am a python beginner, so apologize if this is not so clear, or a dump question. Thank you in advance for your time, and any help you can give. =)

9000
  • 39,899
  • 9
  • 66
  • 104
  • 3
    You can make a function that contains those three loops. – Blender Jul 03 '13 at 01:54
  • It seems that you are looking for `goto` statement; python does not have it. You can achieve the desired outcome by using functions. – Akavall Jul 03 '13 at 02:12
  • If you posted your 3-loop code fragment, we'd have easier time helping you solve your problem. – 9000 Jul 03 '13 at 02:39

2 Answers2

2

You could wrap what ever piece of functionality you have in a function or method, then simply call the function/method whenever you need it.

def print_ten():
    n = 1
    while n <= 10:
        print n
        n = n + 1

print_ten()
print_ten()
John
  • 13,197
  • 7
  • 51
  • 101
1

see Breaking out of nested loops

breaking out of all inner loops and evaluting all inner values in your outer while, should give the desired result.

Community
  • 1
  • 1
j_mcnally
  • 6,928
  • 2
  • 31
  • 46