python 中的重启功能

Restart function in python

我正在尝试制作一个重启功能,这样当您得到该功能的答案时,您可以选择使用新数字获得新答案或直接关闭它。

我尝试使用 def main(),最后又使用 main(),但它不起作用。

所以我在 answer print 之后用我的 yeslist 做了一个重启功能。 , 但因为我不知道要填写什么,在 if restart in yeslist 下我无法重新启动。那么我该如何管理呢?

   #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 循环中。

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

先说几点:

  1. 你不需要;在 Python
  2. 做 return
  3. 时不需要刹车片

很简单,把"main"程序放在一个while循环里,想走就break即可。

一个问题:你现在有 2 个循环(while 和 for)。所以我所做的是添加一个布尔值(do_break)。如果为真,则游戏结束:

# imports, functions and so on here
while True:
    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 = 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 not restart in yeslist:
                # if your input is NOT in yeslist, break
                do_break = True
                break  # Leave the for loop

    # If its breaked it now continues here
    if do_break:
        break  # Break again to leave while loop too

编辑:

我不建议用函数来做,因为递归!

要重复您要遵循此通用框架的过程。

  1. 定义您的 desired/acceptable 回复
  2. 将您的输入变量设置为您接受的回复中的内容
  3. 开始循环while您的输入变量在您的回复中
  4. 在循环内执行您的过程
  5. 循环中的最后一件事,获取用户的输入以用于确定是否继续。
    yeslist = ['y','yes','more']
    continue = 'y'
    while continue in yeslist:
        '''do your process here'''
        continue = input("another?")

尝试这样做:

import os
import sys

def restart():
    os.execl(sys.executable, sys.executable, *sys.argv)

并且每次要重新启动 运行 函数 restart()