Restart Function In Python
I am trying to make a restart function, so that when you have the answer of the function you can choose to get a new answer with new numbers or just close it. i tried with def main
Solution 1:
You should put all the code you want to repeat in a while loop.
#import required modula#import math#from math import sin, piimport math
#list for answers
yeslist = ["yes", "y", "yeah" , "oke"]
#function to calculate x**3deff(x):
u = x**3return(u)
#return math.sqrt(x) #function #Function#function for taking positive integer onlydefpositiveinput(message):
whileTrue:
try:
u= int(input(message))
if u<= -1:
raise ValueError
#return the value of uelif u>=0:
return u
breakexcept ValueError:
print("oops!! That was no valid number. Try again... ")
restart = "yes"while restart in yeslist:
a = positiveinput("What is the lowerlimit?:") #2
b = positiveinput("What is the upperlimit?:") #6
n = positiveinput("How many division intervals do you want?:")
#formula to calculate dx
dx = float ((b-a)/n)
xi = a;
Sum = 0;
for i inrange(n):
xi = xi+dx
Sum = Sum + f(xi)
#to get only the answer instead of (n * answers)if i==n-1:
print("The surface under the line is %.2f"%(Sum*dx))
restart = input("do you want to start again")
exit()
Solution 2:
to repeat a process you want to follow this general framework.
- define your desired/acceptable responses
- set your input variable to something in your accepted responses
- start a loop
while
your input variable is in your responses - inside the loop do your process
- last thing in you loop, get input from the user to use for determining whether to continue.
yeslist = ['y','yes','more']
continue = 'y'whilecontinuein yeslist:
'''do your process here'''continue = input("another?")
Solution 3:
Try doing this:
import os
import sys
def restart():
os.execl(sys.executable, sys.executable, *sys.argv)
And every time you want to restart run the function restart()
.
Solution 4:
Some things first:
- You dont need ; in Python
- You dont need the brakets when doing a return
Its simple, you can just put the "main" program in a while Loop and break if you want to leave.
One problem: You have 2 loops now (while and for). So what I did is added a boolean (do_break). If its true, the game ends:
# imports, functions and so on herewhileTrue:
a = positiveinput("What is the lowerlimit? ") # 2
b = positiveinput("What is the upperlimit? ") # 6
n = positiveinput("How many division intervals do you want? ")
do_break = False# formula to calculate dx
dx = float((b - a) / n)
xi = a
Sum = 0for i inrange(n):
xi = xi + dx
Sum = Sum + f(xi)
# to get only the answer instead of (n * answers)if i == n - 1:
print("The surface under the line is %.2f" % (Sum * dx))
restart = input("Do you want to start again? ")
ifnot restart in yeslist:
# if your input is NOT in yeslist, break
do_break = Truebreak# Leave the for loop# If its breaked it now continues hereif do_break:
break# Break again to leave while loop too
Edit:
I would NOT recommend doing it with functions, because of recursion!
Post a Comment for "Restart Function In Python"