How to make python code run again
I'm trying to restart a program using an if-test based on the input from the user. This code doesn't work, but it's approximately what I'm after:
What I'm trying to do is:
I got really close to a solution with a "while true" loop, but the program either just restarts no matter what you press (except n), or it quits no matter what you press (except y). Any ideas?
asked Feb 16, 2013 at 4:47
This line will unconditionally restart the running program from scratch:
One of its advantage compared to the remaining suggestions so far is that the program itself will be read again. This can be useful if, for example, you are modifying its code in another window. answered May 14, 2015 at 20:57
jlliagrejlliagre 28.9k6 gold badges58 silver badges70 bronze badges 9 Try this:
The inner while loop loops until the input is either
answered Feb 16, 2013 at 4:54
VolatilityVolatility 30k10 gold badges79 silver badges88 bronze badges 0 Using one while loop:
Jason 331 gold badge3 silver badges13 bronze badges answered Feb 16, 2013 at 5:11
rootroot 71.9k25 gold badges104 silver badges119 bronze badges 3 You can do this simply with a function. For example:
Of course you can change a lot of things here. What is said, what the script will accept as a valid input, the variable and function names. You can simply nest the entire program in a user-defined function (Of course you must give everything inside an extra indent) and have it restart at anytime using this line of code:
retnikt 5666 silver badges17 bronze badges answered Jun 10, 2015 at 15:34
It's WillemIt's Willem 4863 gold badges5 silver badges14 bronze badges 1 Here's a fun way to do it with a decorator:
Ultimately, I think you need 2 while loops. You need one loop bracketing the portion which prompts for the answer so that you can prompt again if the user gives bad input. You need a second which will check that the current answer is answered Feb 16, 2013 at 4:54
mgilsonmgilson 288k60 gold badges601 silver badges674 bronze badges It is very easy do this
Basically,
in this the while loop will run the program again and again because while loops run if the condition is
this will look that if there is the 'no' word in the input and if there is then it will break the loop and the program will quit answered Apr 15, 2021 at 6:22
PrabhavDevoPrabhavDevo 1,3406 silver badges21 bronze badges 1 |