0

我正在写一个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();

现在的问题是,一旦输入了要运行的功能,程序就会关闭。我对 python 比较陌生,但对编程不熟悉。这也是编码方式有什么问题(即草率编码),请告诉我。

4

2 回答 2

4

Your input is probably String (e.g. "6") instead of the number 6.

In general, I think that your code is unnecessary long, and breaks the Don't Repeat Yourself principle. For starters, you can ask for the two numbers in a single place, and then call the relevant function to perform the relevant operation.

A more concise design would use Python operators:

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

You can ask for the function type, then call the relevant function (see Lev's answer).

There interesting case is sqr, which takes a single argument, instead if two. This can be solved by specifying the number of arguments each function takes:

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

The solution is now simple - ask for the function number, ask for the right number of arguments, and call funcs[input_number][0].

This idea can be elaborated, so that the function name is also stored:

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)]

Now you program should look like (pseudocode):

 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
于 2012-04-04T10:48:02.747 回答
0

正如亚当指出的那样,问题在于您没有转换funcint. 由于您还询问有关代码组织的建议,我可以建议以下内容来摆脱堆叠的elif条款:

functions = [plus, minus, divide, multiply, power, square]
try:
    func = int(input())
    functions[func-1]()
except:
    print("Please choose an existing function number.")
    exit()
于 2012-04-04T10:53:59.293 回答