Python has control flow goto instead of goto . One implementation of the control flow is the Python while . You can give it a boolean condition (booleans either True or False in Python), and the loop will run several times until this condition becomes false. If you want to loop forever, all you have to do is start an endless loop.
Be careful if you decide to run the following code sample. Press Control + C in your shell while it is running, if you ever want to kill the process. Please note that this process must be in the foreground for this to work.
while True:
The line # do stuff here is just a comment. He does nothing. pass is just a placeholder in python, which basically says "Hi, I am a line of code, but skip me because I am not doing anything."
Now let me say that you want to repeatedly ask the user to enter information forever and always and only exit the program if the user enters the "q" character to exit.
You can do something like this:
while True: cmd = raw_input('Do you want to quit? Enter \'q\'!') if cmd == 'q': break
cmd will simply store all user inputs (the user will be prompted to enter something and press enter). If cmd stores only the letter "q", the code will be forced to break from the closed loop. The break statement avoids any loop. Even endless! It is very useful to know if you want to ever program user applications that often run on endless loops. If the user does not enter exactly the letter "q", the user will simply be requested repeatedly and indefinitely until the process is killed by force or the user decides that he has enough of this annoying program and just wants to exit.
Shashank
source share