2016-12-01 5 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

あなたのメインプログラムは輸入で構成され、 printステートメント次に、2つの関数を定義しますが、呼び出すことはありません。

メインプログラムから、xのような有用なものを呼び出す必要があります。さらに、ユーザー照会ロジックをxからメインプログラムに移動することをお勧めします。ユーザータイプを "start()"にすると便利なことはありません。これはPythonコマンドではなく文字列として提供されます。

いずれの関数も呼び出していません。

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

私は強くあなたが一人で外殻を開始することをお勧めします:あなたは繰り返すことができることを確認してくださいあなたは、このような外側(メインプログラム)の構造のものが必要。次に、二次ケースのみを追加します。あなたが高次のものを追加する前に動作することを確認してください。

0

あなたはあなたの中にstart機能あなたの変数を割り当てるには、forループを次のように使用することができます。ユーザーがタイプquit場合は、各変数に対して

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) 

この方法は、プログラムが終了します。あなたはそれが実行されることを確認するために、あなたの一番下のコードで次のように配置する必要がありますのでstartx関数が呼び出されていない元のコードで

while True: 
    start() 
    x() 
関連する問題