Is there a way to repeat a script in Python, as if from a menu? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

24th Dec 2018, 9:37 PM
Irelae
Irelae - avatar
4 Answers
+ 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.
24th Dec 2018, 10:24 PM
Toni Isotalo
Toni Isotalo - avatar
+ 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!)
24th Dec 2018, 10:00 PM
HonFu
HonFu - avatar
+ 2
I’ll try both of these, thank you so much!
24th Dec 2018, 11:18 PM
Irelae
Irelae - avatar
+ 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'): ...
25th Dec 2018, 8:22 AM
HonFu
HonFu - avatar