diff --git a/sample/calculator.py b/sample/calculator.py index 4da3c60..a82b26d 100644 --- a/sample/calculator.py +++ b/sample/calculator.py @@ -9,7 +9,7 @@ def multiply(a, b): def divide(a, b): if b == 0: - return "Cannot divide by zero!" + raise ValueError("Cannot divide by zero!") return a / b diff --git a/sample/file_utils.py b/sample/file_utils.py index 0a17c47..219dd7e 100644 --- a/sample/file_utils.py +++ b/sample/file_utils.py @@ -1,11 +1,20 @@ -def read_file(filepath): - with open(filepath, "r") as f: - data = f.read() - return data +def read_file(filepath: str) -> str: + try: + with open(filepath, "r") as f: + return f.read() + except FileNotFoundError: + print(f"File not found: {filepath}") + return "" + except IOError as e: + print(f"Error reading file {filepath}: {e}") + return "" -def write_file(filepath, content): - with open(filepath, "w") as f: - f.write(content) +def write_file(filepath: str, content: str) -> None: + try: + with open(filepath, "w") as f: + f.write(content) + except IOError as e: + print(f"Error writing to file {filepath}: {e}") # def read_file(filepath: str) -> str: diff --git a/sample/user.py b/sample/user.py index b8f101f..5f4bd84 100644 --- a/sample/user.py +++ b/sample/user.py @@ -1,23 +1,22 @@ class User: - def __init__(self, username, email): + def __init__(self, username: str, email: str): self.username = username self.email = email self.logged_in = False - def login(self): + def login(self) -> None: self.logged_in = True - def logout(self): + def logout(self) -> None: self.logged_in = False - def is_logged_in(self): + def is_logged_in(self) -> bool: return self.logged_in - def update_email(self, new_email): + def update_email(self, new_email: str) -> None: if "@" not in new_email: - print("Invalid email address!") - else: - self.email = new_email + raise ValueError("Invalid email address") + self.email = new_email # class User: