How do you loop back to the beginning of a program python?
I'm trying to figure out how to make Python go back to the top of the code. In SmallBasic, you do Show
But I can't figure out how you do that in Python :/ Any ideas anyone? The code I'm trying to loop is this
So basically, when the user finishes their conversion, I want it to loop back to the top. I still can't put your loop examples into practise with this, as each time I use the def function to loop, it says that "op" is not defined.
codeforester 35.6k15 gold badges100 silver badges125 bronze badges asked Sep 13, 2013 at 17:20
0 Use an infinite loop:
This certainly can apply to your
If you were to add an option to quit as well, that could be:
for example. answered Sep 13, 2013 at 17:22
Martijn Pieters♦Martijn Pieters 986k274 gold badges3877 silver badges3236 bronze badges 3 Python, like most modern programming languages, does not support "goto". Instead, you must use control functions. There are essentially two ways to do this. 1. Loops An example of how you could do exactly what your SmallBasic example does is as follows:
It's that simple. 2. Recursion
Note on Recursion: Only do this if you have a specific number of times you want to go back to the beginning (in which case add a case when the recursion should stop). It is a bad idea to do an infinite recursion like I define above, because you will eventually run out of memory! Edited to Answer Question More Specifically
answered Sep 13, 2013 at 17:24
River TamRiver Tam 2,9063 gold badges31 silver badges47 bronze badges 14 You can easily do it with loops, there are two types of loops For Loops:
While Loops:
Each of these loops print "Hello World" five times answered Sep 13, 2013 at 17:27
K DawGK DawG 12.8k9 gold badges33 silver badges65 bronze badges Python has control flow statements instead of
Be careful if you decide to run the following example code. Press Control+C in your shell while it is running if you ever want to kill the process. Note that the process must be in the foreground for this to work.
The line Now let's say you want to repeatedly ask the user for input forever and ever, and only exit the program if the user inputs the character 'q' for quit. You could do something like this:
answered Sep 13, 2013 at 17:23
ShashankShashank 13.4k5 gold badges35 silver badges61 bronze badges 3 write a for or while loop and put all of your code inside of it? Goto type programming is a thing of the past. https://wiki.python.org/moin/ForLoop answered Sep 13, 2013 at 17:22
A.J.A.J. 815 bronze badges You need to use a while loop. If you make a while loop, and there's no instruction after the loop, it'll become an infinite loop,and won't stop until you manually stop it. answered Sep 13, 2013 at 17:24
answered Jan 12, 2015 at 13:54
How do you return to the beginning of a loop in Python?The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.
How do you repeat a Python program?Put the whole code in a while True loop and at the end ask the user if they want to repeat. If not, break the loop.
How do you go back to a specific line in Python?Python does not allow you to go back to a specific line number, and even if it did, you should not take advantage of that capability because it results in unmaintainable programs.
|