如何在启动后立即关闭程序?
我希望用户使用列出的功能,并允许用户关闭窗口或在完成后终止进程。
import math
print("use start() to input equation and x() to input x's")
def start():
#ask for intial variables
global t
global a
global b
global c
global d
global e
global f
global g
t = int(input("highest degree coefficient: "))
a = int(input("a: "))
b = int(input("b: "))
c = int(input("c: "))
d = int(input("d: "))
e = int(input("e: "))
f = int(input("f: "))
g = int(input("g: "))
def x():
again = "yes"
#runs if it is a quadratic
if t == 2:
#asks for x input
x = float(input("x: "))
#calculates
ans = ((a) * x ** 2) + (b * x) + c
#prints result
print("f(x):",ans)
#asks if there is more
again = input("again?: ")
again = again.lower()
while again != "no":
x = float(input("x: "))
ans = ((a) * x ** 2) + (b * x) + c
print("f(x):",ans)
again = input("again?: ")
again = again.lower()
#runs if it is a cubic
elif t == 3:
#asks for x input
x = float(input("x: "))
#calculates
ans = ((a) * x ** 3) + ((b) * x ** 2) + (c * x) + d
#prints result
print("f(x):",ans)
#asks if there is more
again = input("again?: ")
again = again.lower()
while again != "no":
x = float(input("x: "))
ans = ((a) * x ** 3) + ((b) * x ** 2) + (c * x) + d
print("f(x):",ans)
again = input("again?: ")
again = again.lower()
elif t == 4:
#asks for x input
x = float(input("x: "))
#calculates
ans = ((a) * x ** 4) + ((b) * x ** 3) + ((c) * x ** 2) + (d * x) + e
#prints result
print("f(x):",ans)
#asks if there is more
again = input("again?: ")
again = again.lower()
while again != "no":
x = float(input("x: "))
ans = ((a) * x ** 4) + ((b) * x ** 3) + ((c) * x ** 2) + (d * x) + e
print("f(x):",ans)
again = input("again?: ")
again = again.lower()
elif t == 5:
#asks for x input
x = float(input("x: "))
#calculates
ans = ((a) * x ** 5) + ((b) * x ** 4) + ((c) * x ** 3) + ((d) * x ** 2) + (e * x) + f
#prints result
print("f(x):",ans)
#asks if there is more
again = input("again?: ")
again = again.lower()
while again != "no":
x = float(input("x: "))
ans = ((a) * x ** 5) + ((b) * x ** 4) + ((c) * x ** 3) + ((d) * x ** 2) + (e * x) + f
print("f(x):",ans)
again = input("again?: ")
again = again.lower()
elif t == 6:
#asks for x input
x = float(input("x: "))
#calculates
ans = ((a) * x ** 6) + ((b) * x ** 5) + ((c) * x ** 4) + ((d) * x ** 3) + ((e) * x ** 2) + (f * x) + g
#prints result
print("f(x):",ans)
#asks if there is more
again = input("again?: ")
again = again.lower()
while again != "no":
x = float(input("x: "))
ans = ((a) * x ** 6) + ((b) * x ** 5) + ((c) * x ** 4) + ((d) * x ** 3) + ((e) * x ** 2) + (f * x) + g
print("f(x):",ans)
again = input("again?: ")
again = again.lower()
else:
print("coming soon")
答案 0 :(得分:0)
您可以使用以下for循环在start
函数中分配变量:
def start():
#ask for intial variables
global t
global a
global b
global c
global d
global e
global f
global g
for letter in "tabcdefg":
if letter == "t":
var = input("highest degree coefficient: ")
else:
var = input(letter + ": ")
if var == "quit":
quit()
else:
globals()[letter] = int(var)
对于每个变量,如果用户键入quit
,程序将退出。
在原始代码中,未调用start
和x
函数,因此您需要在代码的底部放置以下内容以确保其执行:
while True:
start()
x()
答案 1 :(得分:0)
您的主程序包含导入和打印声明。然后它定义了两个函数 - 但从不调用它们 - 并退出。
您需要从主程序中调用一些有用的东西,例如函数 x 。更好的是,将用户查询逻辑从 x 移动到主程序。让用户键入" start()"不会做任何有用的事情:它以字符串形式出现,而不是Python命令。
您的代码中没有任何内容调用任何函数。你需要一个外部(主程序)结构,如下所示:
again = "yes"
while again != "no":
start()
x()
again = input("again?: ")
again = again.lower()
我强烈建议您单独从外壳开始:确保您可以重复。接下来,仅添加二次情形;确保在添加高阶数之前有效。