我正在寫一個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