2016-12-01 42 views
1

如何让程序在启动后立即关闭?保持程序的功能列表立即关闭

我希望用户使用列出的功能,并允许用户关闭窗口或在完成后终止进程。

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

你的主要程序包括进口和打印声明。然后它定义了两个函数 - 但从来没有调用它们 - 然后退出。

你需要从主程序中调用一些有用的东西,比如函数x。更好的是,将用户查询逻辑从x移动到主程序。让用户输入“start()”将不会起任何作用:它以字符串形式出现,而不是Python命令。

代码中的任何内容都不会调用这两个函数。您需要一个外部(主程序)的结构是这样的:

again = "yes" 
while again != "no": 
    start() 
    x() 
    again = input("again?: ") 
    again = again.lower() 

我强烈建议你开始单独外壳:确保你可以重复。接下来,只添加二次方案;请确保在添加更高级别之前工作。

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,程序将退出。

在你的原代码startx功能不调用,所以你需要把下面的在你的代码的底部,以确保其执行:

while True: 
    start() 
    x()