我正在写一个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相对来说比较新,但不适合编程。这种编码的方式也是错的(即马虎编码),请告诉我。
你知道你并不需要把分号在每个字符串的结束,你不是吗? – 2012-04-04 10:47:38
你也不需要在每个函数的末尾都显式返回'return'。从函数返回当它到达最后时会发生什么。另外,在从exit()调用'inputs()'的时候,它会工作一段时间,这很麻烦,并且暗示了对函数的工作原理的进一步误解。 – 2012-04-04 10:56:41
无论如何,为了找出程序实际发生了什么问题,您应该从一个已经存在的命令窗口(当程序退出时不会关闭)运行它。 – 2012-04-04 10:57:49