diff --git a/09/arithmetic.py b/09/arithmetic.py new file mode 100644 index 0000000000000000000000000000000000000000..c7c08ea28166f0282ccf88c110287cfb1c316210 --- /dev/null +++ b/09/arithmetic.py @@ -0,0 +1,46 @@ +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))