Python函数不会启动

时间:2012-04-04 10:46:02

标签: python function python-3.x

我正在写一个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的新手,但不是编程。编码的方式也有问题(即草率编码),请告诉我。

2 个答案:

答案 0 :(得分:4)

您的输入可能是字符串(例如"6"),而不是数字6

一般来说,我认为您的代码不必要很长,并且违反了Don't Repeat Yourself原则。对于初学者,您可以在一个地方要求两个号码,然后调用相关功能来执行相关操作。

更简洁的设计将使用Python运算符:

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

您可以询问功能类型,然后调用相关功能(参见Lev的回答)。

有趣的案例是sqr,它只取一个参数,而不是两个。这可以通过指定每个函数所需的参数个数来解决:

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

解决方案现在很简单 - 询问函数编号,询问正确数量的参数,然后调用funcs[input_number][0]

可以详细说明这个想法,以便存储函数名称:

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

答案 1 :(得分:0)

正如亚当所说,问题在于你没有将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()