问题
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()
回答1:
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()
回答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'
while continue in yeslist:
'''do your process here'''
continue = input("another?")
回答3:
This will solve your problem. Just add while True, so that it starts again once all I/Os are complete. Hope it helped.
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... ")
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?:")
#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))
回答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 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
Edit:
I would NOT recommend doing it with functions, because of recursion!
来源:https://stackoverflow.com/questions/62248430/restart-function-in-python