我正在写一个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 比较陌生,但对编程不熟悉。这也是编码方式有什么问题(即草率编码),请告诉我。