Math operations with one line input. Any suggestions to improve this code?

Viewed 15

'''I wrote this program so I could do the math operations like I do in a terminal. just add the numbers and the operators and hit enter. that will give me the result. With this code same can be done. I am sharing this so I can get some feedback on how to make this more efficient, add more functionality to it, etc...'''

    # Basic operator functions
    def add(num1, num2):
        return num1 + num2


    def sub(num1, num2):
        return num1 - num2


    def mul(num1, num2):
        return num1 * num2


    def dev(num1, num2):
        return num1 / num2


    operators = {'+': add, '-': sub, '*': mul, '/': dev}


    def splitter(text):
        """Function to split an input string containing number and operators
        in a list separating numbers and operators in given sequence"""
        numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', "."]
        delimiter = ["+", "-", "*", "/"]
        elist = []
        num = ""

        for char in text:
            if char in numbers:
                num += char

            elif char in delimiter:
                elist.append(float(num))
                elist.append(char)
                num = ""
            elif char == " ":
                continue
            else:
                print(f"Invalid input: {char}")
                break

        elist.append(float(num))
        return elist


    def calculator(splitter1):
        result = splitter1
        # print(result)
        total = 0
        while len(result) > 1:

            if "/" in result:
                o_index = result.index("/")
                n1 = (o_index - 1)
                n2 = (o_index + 1)
                total = dev(result[n1], result[n2])
                result[o_index] = total
                result.pop(n1)
                result.pop(o_index)
            elif "*" in result:
                o_index = result.index("*")
                n1 = (o_index - 1)
                n2 = (o_index + 1)
                total = mul(result[n1], result[n2])
                result[o_index] = total
                result.pop(n1)
                result.pop(o_index)
            elif "+" in result:
                o_index = result.index("+")
                n1 = (o_index - 1)
                n2 = (o_index + 1)
                total = add(result[n1], result[n2])
                result[o_index] = total
                result.pop(n1)
                result.pop(o_index)
            elif "-" in result:
                o_index = result.index("-")
                n1 = (o_index - 1)
                n2 = (o_index + 1)
                total = sub(result[n1], result[n2])
                result[o_index] = total
                result.pop(n1)
                result.pop(o_index)
            else:
                continue
            # print(result)
        return total


    repeat = "y"

    while repeat == "y":
        cal = input('calc: ')
        calculation = calculator(splitter(cal))
        print(calculation)
        repeat = input("Would you like to continue to a new calculation? type 'y' to continue: ").lower()
    else:
        print("Thank you!")
0 Answers
Related