Search code examples
pythonrestart

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() and then in the end again with main() but it is not working.

so i have made after the ,answer print, a restart function with my yeslist. , but beacuse i dont know what to fill in, under if restart in yeslist i cant get my restart. So how may i manage this?

   #import required modula
#import math
#from math import sin, pi
import math

#list for answers 
yeslist = ["yes", "y", "yeah" , "oke"]
#function to calculate x**3
def f(x):
    u = x**3
    return(u)
    #return math.sqrt(x) #function 
     #Function



#function for taking positive integer only
def positiveinput(message):
    while True:
        try:
            u= int(input(message))
            if u<= -1:
                raise ValueError
            #return the value of u
            elif u>=0:
                return u
            break
        except ValueError:
            print("oops!! That was no valid number. Try again... ")

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 in range(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")
        if restart in yeslist :
            input()
        else:
            exit()

Solution

  • You should put all the code you want to repeat in a while loop.

    #import required modula
    #import math
    #from math import sin, pi
    import math
    
    #list for answers 
    yeslist = ["yes", "y", "yeah" , "oke"]
    #function to calculate x**3
    def f(x):
        u = x**3
        return(u)
        #return math.sqrt(x) #function 
         #Function
    
    
    
    #function for taking positive integer only
    def positiveinput(message):
        while True:
            try:
                u= int(input(message))
                if u<= -1:
                    raise ValueError
                #return the value of u
                elif u>=0:
                    return u
                break
            except 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 in range(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()