Untitled

 avatar
unknown
plain_text
10 months ago
792 B
15
Indexable
    def calculate():
        A = input("Input your operation: ")

        for op in ['+', '-', '*', '/']:
            if op in A:
                nums = A.split(op)
                first = nums[0].strip()
                second = nums[1].strip()
                operator = op
                break
        else:
            print("Invalid input")
            return
        
        firstNum = float(first)
        secondNum = float(second)

        operations = {
            '+' : lambda x, y: x + y,
            '-' : lambda x, y: x - y,
            '*' : lambda x, y: x * y,
            '/' : lambda x, y: x / y if y != 0 else "Error: Cannot divide by 0"
        }

        result = operations[operator](firstNum, secondNum)
        print(result)
    calculate()
Editor is loading...
Leave a Comment