Commit 6bfd5195 authored by Adam Blank's avatar Adam Blank
Browse files

autocommit

parent 0605c2e2
Showing with 46 additions and 0 deletions
+46 -0
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))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment