Commit a334f446 authored by Adam Blank's avatar Adam Blank
Browse files

move tests

No related merge requests found
Showing with 21 additions and 5 deletions
+21 -5
......@@ -10,10 +10,15 @@ def apply_operation(op: str, a: float, b: float) -> float:
return a / b
elif op == '*':
return a * b
else:
raise NotImplementedError("That operation is not implemented yet!")
def as_number(x: str) -> float:
try:
return float(x)
except ValueError:
raise TypeError("not a float")
def as_int(x: float) -> int | float:
......@@ -28,8 +33,8 @@ def compute_answer(expr: str) -> float:
while '' in pieces:
pieces.remove('')
if len(pieces) != 3:
print("You didn't provide two operands and an operation!")
if len(pieces) > 3:
raise ValueError("You provided too many arguments!")
a: float = as_number(pieces[0])
b: float = as_number(pieces[2])
......@@ -42,5 +47,16 @@ def compute_answer(expr: str) -> float:
while True:
expr: str = input("Enter a mathematical expression: ")
try:
ans: float = compute_answer(expr)
print(">> " + str(ans))
except NotImplementedError:
print(colored("That's not a valid operation!", "red"))
except IndexError:
print(colored("That's not enough arguments!", "red"))
except ValueError as e:
print(colored("I only support simple math. :(", "blue"))
except TypeError as e:
print(colored("Variable are too hard for me to handle. :(", "yellow"))
except ZeroDivisionError:
print("bah.")
\ No newline at end of file
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