簡體   English   中英

Python function 無法啟動

[英]Python function won't start

我正在寫一個 python 計算器,代碼如下:

#Python Calculator

import sys;
import cmath;

def plus():
    num1 = float(input("Input the first number: "));
    num2 = float(input("Input the second number: "));
    ans = (num1 + num2);
    print(ans);
    exit();
    return;

def minus():
    num1 = float(input("Input the first number: "));
    num2 = float(input("Input the second number: "));
    ans = (num1 - num2);
    print(ans);
    exit();
    return;

def divide():
    num1 = float(input("Input the first number: "));
    num2 = float(input("Input the second number: "));
    ans = (num1 / num2);
    print(ans);
    exit();
    return;

def multiply():
    num1 = float(input("Input the first number: "));
    num2 = float(input("Input the second number: "));
    ans = (num1 * num2);
    print(ans);
    exit();
    return;

def power():
    num1 = float(input("Input the number: "));
    num2 = float(input("Input the power: "));
    ans = cmath.pow(num1, num2);
    print(ans);
    exit();
    return;

def square():
    num1 = float(input("Input the number: "));
    ans = cmath.sqrt(num1);
    print(ans);
    exit();
    return;

def inputs():
    print("Select which function you would like to use:");
    print("1 for Plus");
    print("2 for Minus");
    print("3 for Divide");
    print("4 for Multiply");
    print("5 for Power");
    print("6 for Square Root");
    func = input();

    if func == 1:
        plus();
    elif func == 2:
        minus();
    elif func == 3:
        divide();
    elif func == 4:
        multiply();
    elif func == 5:
        power();
    elif func == 6:
        square();
    return;

def exit():
    exit = str(input("Run again? y/n: "));
    if exit == "Y" or exit == "y":
        inputs();
        print ("");
    elif exit == "N" or exit == "n":
        sys.exit();
    else:
        exit();
    return;

print ("Python Calculator");
print("");
inputs();

現在的問題是,一旦您輸入了要運行的 function,程序就會關閉。 我對 python 比較陌生,但不是編程。 另外,這種編碼方式有什么問題(即草率編碼)請告訴我。

您的輸入可能是字符串(例如"6" )而不是數字6

總的來說,我認為您的代碼不必要地長,並且違反了不要重復自己的原則。 對於初學者,您可以在一個地方詢問兩個號碼,然后撥打相關的function進行相關操作。

更簡潔的設計將使用 Python 運算符:

funcs=[operator.add, operator.sub, operator.div, 
       operator.mul, operator.pow, your_square_function]

您可以詢問 function 類型,然后撥打相關的 function(請參閱 Lev 的回答)。

有趣的情況是sqr ,它接受一個參數,而不是兩個參數。 這可以通過指定 arguments 的數量來解決,每個 function 需要:

funcs=[(operator.add, 1), (operator.sub, 2), (operator.div, 2),
       (operator.mul, 2), (operator.pow, 2), (your_square_function, 1)]

解決方案現在很簡單 - 詢問 function 號碼,詢問正確的號碼 arguments,然后調用funcs[input_number][0]

這個思路可以細化一下,這樣把function這個名字也存儲起來:

funcs=[("Plus", operator.add, 1),   ("Minus", operator.sub, 2), 
       ("Divide", operator.div, 2), ("Multiply", operator.mul, 2), 
       ("Power", operator.pow, 2),  ("Square root", your_square_function, 1)]

現在你的程序應該看起來像(偽代碼):

 for f in funcs:
      print id, function_name
 ask for id
 ask for relevant number of arguments
 run funcs[id] with relevant number of arguments

正如 Adam 指出的那樣,問題是您沒有將func轉換為int 由於您還詢問有關代碼組織的建議,我可以建議以下內容來擺脫堆疊的elif子句:

functions = [plus, minus, divide, multiply, power, square]
try:
    func = int(input())
    functions[func-1]()
except:
    print("Please choose an existing function number.")
    exit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM