Skip to content
← Back to Community
How to restart a python program ?
Profile icon
TejasvMaheshwar

How to restart a python program also
if there is any way i can ask the user to type in the correct inputs

Answered by Kopamed [earned 5 cycles]
View Answer
Voters
Profile icon
AGATETRAUBERGA
Profile icon
SnehaAT
Profile icon
ChristopherWoo5
Profile icon
unknownhuman
Profile icon
stubaduble
Profile icon
Kopamed
Profile icon
NarwhalFanatic
Profile icon
TejasvMaheshwar
Comments
hotnewtop
Profile icon
Kopamed

Not number:

while True: answer = input("Yes or no?(y/n)") if answer.lower() == "y" or answer.lower() == "n": break

For numbers:

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!")
Profile icon
TejasvMaheshwar

@Kopamed i do understand your hardwork to this but i cannot understand any of this ......cuz am a newbie

and what does .lower() mean??

Profile icon
Kopamed

@TejasvMaheshwar
imagine you have a variable

a_string = "HeLlO"

when you add .lower() to a, all the letters in that string become lower cased. .upper() does the opposite - alll letters will be upper case

Profile icon
TejasvMaheshwar

@Kopamed thanks a lot sir

Profile icon
TejasvMaheshwar

@Kopamed what the use of break?

Profile icon
Kopamed

@TejasvMaheshwar No problem dude

Profile icon
Kopamed

@TejasvMaheshwar It stops a

while

loop

Profile icon
TejasvMaheshwar

@Kopamed
if answer.lower() == n
then how to restart the program again?

Profile icon
Kopamed

@TejasvMaheshwar for which purpose do you need to restart the project?

Profile icon
Kopamed

@TejasvMaheshwar I see you are making a calculator. I made a unbreakable calculator a while back. If you want to see how it works, tell me

Profile icon
TejasvMaheshwar

@Kopamed see am making a calci if someone types in like abcd in instead of typing in an number i want the program the to restart till the correct input is been made

also it would be really helpful if you can share some sort of id through which i can contact you .....gmail would work

Profile icon
Kopamed

@TejasvMaheshwar You dont want the program to restart. Wait like 30 mins, ill send you my version of the calculator

Profile icon
TejasvMaheshwar

@Kopamed am intrested .....tell everything about it

Profile icon
TejasvMaheshwar

@Kopamed thanks

Profile icon
Kopamed
Profile icon
TejasvMaheshwar

@Kopamed thanks again sir

Profile icon
Kopamed
Profile icon
TejasvMaheshwar

@Kopamed print("{0} + {1} = {2}".format(num1, num2, (num1+num2)))

whats this format thing......explain please

Profile icon
TejasvMaheshwar

https://repl.it/@TejasvMaheshwar/[email protected] why is this not restarting like yours???

Profile icon
Kopamed

@TejasvMaheshwar line 21 should be

if Stop.lower() == "n":

or

if Stop == "N" or Stop == "n":
Profile icon
TejasvMaheshwar

@Kopamed Dude you're awesomeee......Thankkkkk youuuuu

Profile icon
Kopamed

@TejasvMaheshwar Have I given the right answer. If not, find the person which has given you the answer and mark their answer is correct to inform people that this question has been answered. (You mark the answer as correct by clicking on the tick mark on the left of the answer)

Profile icon
CircuitSacul

@Kopamed You should specify the exception type, like this:

except TypeError:
Profile icon
Kopamed

@LD1 Ok

Profile icon
CircuitSacul

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

Edit #1:
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")

not

except: print("Invalid input, Try Again") calc()
Profile icon
HackermonDev

You can use try and except. Learn more here: https://www.w3schools.com/python/python_try_except.asp

Profile icon
TejasvMaheshwar

@PDanielY Thanks

Profile icon
StudioHawaii

thanks

Profile icon
TejasvMaheshwar

sir i appreciate your help.....but i cant understand any of it since am a newbie @johno1566

Profile icon
NarwhalFanatic

You could do print("You must type a number between 1 and 2")

Profile icon
TejasvMaheshwar

@NarwhalFanatic What if some lunatic types in a word instead of numbers....

Profile icon
NarwhalFanatic

@TejasvMaheshwar have in if statement that checks if their answer is an integer.

Profile icon
TejasvMaheshwar

thats what am finding
@NarwhalFanatic