From a334f446ed8afc40c307079cb31d14aea1708358 Mon Sep 17 00:00:00 2001 From: "blank@caltech.edu" <blank@caltech.edu> Date: Fri, 25 Oct 2024 16:43:51 +0000 Subject: [PATCH] move tests --- 09/arithmetic.py | 26 +++++++++++++++---- .../test_mystery.py | 0 2 files changed, 21 insertions(+), 5 deletions(-) rename tests/{0.Lecture 11 => 0.Lecture11}/test_mystery.py (100%) diff --git a/09/arithmetic.py b/09/arithmetic.py index c7c08ea..17c3be2 100644 --- a/09/arithmetic.py +++ b/09/arithmetic.py @@ -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: - return float(x) + 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: ") - ans: float = compute_answer(expr) - print(">> " + str(ans)) + 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 diff --git a/tests/0.Lecture 11/test_mystery.py b/tests/0.Lecture11/test_mystery.py similarity index 100% rename from tests/0.Lecture 11/test_mystery.py rename to tests/0.Lecture11/test_mystery.py -- GitLab