在python中重新启动函数



我正在尝试创建一个重新启动函数,以便当您获得该函数的答案时,您可以选择使用新数字获取新答案或只是关闭它。

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

所以我在回答打印之后,用我的 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. 定义您想要/可接受的响应
  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()

首先要做一些事情:

  1. 你不需要 ; 在 Python 中
  2. 退货时不需要刹车

它很简单,如果你想离开,你可以把"主"程序放在一段时间循环和中断。

一个问题:你现在有 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

编辑:

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

最新更新