Python function won't start
Question: 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.


Answer: Your input is probably String (e.g. `"6"`) instead of the number `6`.

In general, I think that your code is unnecessary long, and breaks the [Don't
Repeat Yourself](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself)
principle. For starters, you can ask for the two numbers in a single place,
and then call the relevant function to perform the relevant operation.

A more concise design would use Python operators:

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

You can ask for the function type, then call the relevant function (see Lev's
answer).

There interesting case is `sqr`, which takes a single argument, instead if
two. This can be solved by specifying the number of arguments each function
takes:

    
    
    funcs=[(operator.add, 1), (operator.sub, 2), (operator.div, 2),
           (operator.mul, 2), (operator.pow, 2), (your_square_function, 1)]
    

The solution is now simple - ask for the function number, ask for the right
number of arguments, and call `funcs[input_number][0]`.

This idea can be elaborated, so that the function name is also stored:

    
    
    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)]
    

Now you program should look like (pseudocode):

    
    
     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
    

