Casper2017
Posts: 28
Joined: Thu Mar 23, 2017 12:44 pm

Python 2 looping a program back to the begining

Postby Casper2017 » Fri May 19, 2017 10:20 am

Hi

After writing a program, how is it possible to have it loop back to the beginning.

All examples on the web show how to use loops, but that is with numbers.

I would just like the program to loop back to the beginning of the program.


Thanks
ghans
Posts: 7279
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Python 2 looping a program back to the begining

Postby ghans » Fri May 19, 2017 10:23 am

Code: Select all

import sys

while True:
   try:
      # loop everything inside this block forever
      print "Loop"
      do_things()
      another_thing(arg1,arg2)
   except KeyboardInterrupt:
      # ... or at least until we want to abort via Ctrl-C as usual under Linux
      sys.exit()
Your whole program can be one while loop. Its a common idiom.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org