arithmetic.py 950 Bytes
from termcolor import colored


def apply_operation(op: str, a: float, b: float) -> float:
    if op == '+':
        return a + b
    elif op == '-':
        return a - b
    elif op == '/':
        return a / b
    elif op == '*':
        return a * b


def as_number(x: str) -> float:
    return float(x)


def as_int(x: float) -> int | float:
    if int(x) == x:
        return int(x)
    else:
        return x


def compute_answer(expr: str) -> float:
    pieces: list[str] = expr.split(" ")
    while '' in pieces:
        pieces.remove('')

    if len(pieces) != 3:
        print("You didn't provide two operands and an operation!")

    a: float = as_number(pieces[0])
    b: float = as_number(pieces[2])

    op: str = pieces[1]
    answer: float = apply_operation(op, a, b)

    return as_int(answer)


while True:
    expr: str = input("Enter a mathematical expression: ")
    ans: float = compute_answer(expr)
    print(">> " + str(ans))