python中的重启功能 [英] Restart function in python

查看:50
本文介绍了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.

我尝试使用 def main() 然后最后再次使用 main() 但它不起作用.

i tried with def main() and then in the end again with main() but it is not working.

所以我在打印答案后使用我的 yeslist 进行了重新启动功能.,但是因为我不知道要填写什么,在 if restart in yeslist 下我无法重新启动.那么我该如何管理呢?

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()

推荐答案

你应该把所有你想重复的代码放在一个 while 循环中.

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()

这篇关于python中的重启功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆