'Make calculator using python

I coded calculator using Python like just add the choice, add two numbers and get the output. Also I input reset and terminate option also here.

Here is the code that I tried. But it is not get the output it shows an error.

def add(num1, num2):
    return num1 + num2

def sub(num1, num2):
    return num1 - num2
def mul(num1, num2):
    return num1 * num2
def div(num1, num2):
    return num1 / num2
def power(num1, num2):
    return num1 ** num2
def rem(num1, num2):
    return num1 % num2
def select_op(choice):
    return choice
while True:
    print("Select operation.")
    print("1.Add      : + ")
    print("2.Subtract : - ")
    print("3.Multiply : * ")
    print("4.Divide   : / ")
    print("5.Power    : ^ ")
    print("6.Remainder: % ")
    print("7.Terminate: # ")
    print("8.Reset    : $ ")
    choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
    print(choice)
    if (select_op(choice) == -1):
        print("Done. Terminating")
        exit()
    else:
        num1 = float(input("Enter First Number  : "))
        num2 = float(input("Enter Second Number : "))
            if(select_op(choice) == "+"):
                print(num1, "+", num2, "=", add(num1,num2))
            elif(select_op(choice) == "-"):
                print(num1, "-", num2, "=", sub(num1,num2))
            elif(select_op(choice) == "*"):
                print(num1, "*", num2, "=", mul(num1,num2))
            elif(select_op(choice) == "/"):
                print(num1, "/", num2, "=", div(num1,num2))
            elif(select_op(choice) == "^"):
                print(num1, "^", num2, "=", power(num1,num2))
            elif(select_op(choice) == "%"):
                print(num1, "+", num2, "=", rem(num1,num2))
            elif(select_op(choice) == "$"):
                return True
            else:
              print("Something Went Worng")


Solution 1:[1]

The error message tell every thing. if(select_op(choice) == "+"): IndentationError: unexpected indent Because your indent is wrong, it should be like this.

And your code has more problem than that.

while True:
    print("Select operation.")
    print("1.Add      : + ")
    print("2.Subtract : - ")
    print("3.Multiply : * ")
    print("4.Divide   : / ")
    print("5.Power    : ^ ")
    print("6.Remainder: % ")
    print("7.Terminate: # ")
    print("8.Reset    : $ ")
    choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
    print(choice)
    if (select_op(choice) == -1): #<- choice is String type, you can't compare it to int.
        print("Done. Terminating")
        exit()
    else:
        num1 = float(input("Enter First Number  : "))
        num2 = float(input("Enter Second Number : "))
        if(select_op(choice) == "+"):
            print(num1, "+", num2, "=", add(num1,num2))
        elif(select_op(choice) == "-"):
            print(num1, "-", num2, "=", sub(num1,num2))
        elif(select_op(choice) == "*"):
            print(num1, "*", num2, "=", mul(num1,num2))
        elif(select_op(choice) == "/"):
            print(num1, "/", num2, "=", div(num1,num2))
        elif(select_op(choice) == "^"):
            print(num1, "^", num2, "=", power(num1,num2))
        elif(select_op(choice) == "%"):
            print(num1, "+", num2, "=", rem(num1,num2))
        elif(select_op(choice) == "$"):
            return True #<- Also you can't return outside function
        else:
            print("Something Went Worng")

Solution 2:[2]

elif(select_op(choice) == "$"):
                return True

According to the return command in your code. You must copy the if statement from a function.
Please use break instead of return.

Please check following clip for the unexpected indent issue.

    else:
        num1 = float(input("Enter First Number  : "))
        num2 = float(input("Enter Second Number : "))
        ## dedented following lines
        if(select_op(choice) == "+"):
            print(num1, "+", num2, "=", add(num1,num2))
        elif(select_op(choice) == "-"):
            print(num1, "-", num2, "=", sub(num1,num2))

BTW, this would be better for a calculator.

t = ('+','-','*','/','^','%')      # ,'#','$')
while True:
    print("Select operation.")
    print("1.Add      : + ")
    print("2.Subtract : - ")
    print("3.Multiply : * ")
    print("4.Divide   : / ")
    print("5.Power    : ^ ")
    print("6.Remainder: % ")
    print("7.Terminate: # ")
    print("8.Reset    : $ ")
    choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
    print(choice)
    if (select_op(choice) == -1):
        print("Done. Terminating")
        exit()
    elif choice == '$': break
    elif choice in t:
        num1 = input("Enter First Number  : ")
        num2 = input("Enter Second Number : ")
        print('{} {} {} = {}'.format(num1, choice, num2, eval(num1+choice+num2)) )
    else:
        print("Something Went Worng")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SoulCRYSIS
Solution 2 hochae