I'm writing a python calculator, here is the code:
#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();
Now the problem is, once you have inputted the function you want to run, the program just closes. I am relatively new to python, but not to programming. Also is anything wrong with the way this is coded (i.e. sloppy coding) please tell me.
returnat the end of every function. Returning from a function is what happens anyway when it reaches the end. Also, while calling back toinputs()fromexit()will work for a while, it's messy and suggests further misunderstanding of how functions work.