How to restart a python program ?
How to restart a python program also
if there is any way i can ask the user to type in the correct inputs
while True: answer = input("Yes or no?(y/n)") if answer.lower() == "y" or answer.lower() == "n": break
while True: try: age = int(input("How old are you?")) break except: print("I said your age, not your great-grand-auntie's dog's name!")
I see that people have answered your second question, but not the first. I don't think that there is a way to restart a python program from within the file, unless you put it in a function and just told it to re-run the function, then break the original, like this:
def main(): #main code here #to "restart" program: main() #this just re-runs the program that is currently running exit() #this tells the python program to stop completely main()
If you need a better example just let me know
I just noticed that when you except an error, you also re-run the function. This is not necessary because it is in a while loop and will re-run by itself.
except: print("Invalid input, Try Again")
except: print("Invalid input, Try Again") calc()
You can use try and except. Learn more here: https://www.w3schools.com/python/python_try_except.asp