2012-04-04 66 views
0

我正在写一个python计算器,这里是代码: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相对来说比较新,但不适合编程。这种编码的方式也是错的(即马虎编码),请告诉我。

+6

你知道你并不需要把分号在每个字符串的结束,你不是吗? – 2012-04-04 10:47:38

+0

你也不需要在每个函数的末尾都显式返回'return'。从函数返回当它到达最后时会发生什么。另外,在从exit()调用'inputs()'的时候,它会工作一段时间,这很麻烦,并且暗示了对函数的工作原理的进一步误解。 – 2012-04-04 10:56:41

+0

无论如何,为了找出程序实际发生了什么问题,您应该从一个已经存在的命令窗口(当程序退出时不会关闭)运行它。 – 2012-04-04 10:57:49

回答

4

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

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

更简洁的设计将使用Python运营商:

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

你可以要求函数类型,然后调用相关功能(见利的答案)。

有趣的情况是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

非常感谢,python将“func”变量作为一个字符串输入,所以已经修复了这个程序,现在我需要按照您的建议开发一个更好的设计。再次谢谢你! – jambolina 2012-04-04 11:16:30

+0

你已经选择了正确的语言 - 你会惊讶你的代码会是多么简短和可读。 – 2012-04-04 11:28:27

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() 
+0

这可以扩展到运营商 - 请参阅我的答案。 – 2012-04-04 10:57:25

+3

我会谨慎建议一个python新角色使用'except all'。 – MattH 2012-04-04 10:57:41

+0

@MattH你会写'except(ValueError,IndexError)'吗? – 2012-04-04 11:01:45