+ 1
Is there a way to repeat a script in Python, as if from a menu?
Is there any way I might be able to return a program to I guess what youâd call a âmenuâ, rather than it ending the script whenever it finishes? Iâve been trying to find a way of going about this but I just canât seem to get a good understanding of which functions to use.
4 RĂ©ponses
+ 4
Maybe more like this.
from time import sleep
try:
while True:
#your code here
except KeyboardInterrupt:
#ctrl+c
print('Exiting script');
There's many ways but we can't give a good answer without your whole script.
+ 2
You can write an eternal loop and put your script inside it, like this:
while True:
#your code here
When the script reaches its end, it will start over from the while.
(Try to work with the idea and see how far it gets you!)
+ 2
Iâll try both of these, thank you so much!
+ 2
Toni Isotalo, what do you use sleep for in this case?
I doubt it's a good idea to put your whole code into an exception handler block. You should use 'try' to *isolate* a problematic piece of code.
If you want a possibility to leave the program, you can just define an appropriate condition instead of using True (pseudo code):
while(user hasn't chosen 'end'):
...