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
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()
You can use try and except. Learn more here: https://www.w3schools.com/python/python_try_except.asp
@PDanielY Thanks
thanks
sir i appreciate your help.....but i cant understand any of it since am a newbie @johno1566
You could do print("You must type a number between 1 and 2")
@NarwhalFanatic What if some lunatic types in a word instead of numbers....
@TejasvMaheshwar have in if statement that checks if their answer is an integer.
thats what am finding
@NarwhalFanatic
Not number:
For numbers:
@Kopamed i do understand your hardwork to this but i cannot understand any of this ......cuz am a newbie
and what does .lower() mean??
@TejasvMaheshwar
imagine you have a variable
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
@Kopamed thanks a lot sir
@Kopamed what the use of break?
@TejasvMaheshwar No problem dude
@TejasvMaheshwar It stops a
loop
@Kopamed
if answer.lower() == n
then how to restart the program again?
@TejasvMaheshwar for which purpose do you need to restart the project?
@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
@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
@TejasvMaheshwar You dont want the program to restart. Wait like 30 mins, ill send you my version of the calculator
@Kopamed am intrested .....tell everything about it
@Kopamed thanks
@TejasvMaheshwar Unhackabla calc
@Kopamed thanks again sir
@TejasvMaheshwar np
@Kopamed print("{0} + {1} = {2}".format(num1, num2, (num1+num2)))
whats this format thing......explain please
https://repl.it/@TejasvMaheshwar/[email protected] why is this not restarting like yours???
@TejasvMaheshwar line 21 should be
or
@Kopamed Dude you're awesomeee......Thankkkkk youuuuu
@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)
@Kopamed You should specify the exception type, like this:
@LD1 Ok