# Build a UserDatabase class: # __init__ starts with an empty dict _users # add_user(username, age) — raises ValueError if age is negative, raises TypeError if username isn't a string # get_user(username) — raises KeyError if user doesn't exist # delete_user(username) — raises KeyError if user doesn't exist # Create a custom DuplicateUserError — raised if adding a username that already exists class UserDatabase: def __init__(self): self._users = {} def add_user(self, username: str, age: int) -> str: if age < 0: raise ValueError("Age can't be negative") if not isinstance(username, str): raise TypeError("it should be a string") if username in self._users: raise DuplicateUserError(f"The user: {username} is already present, please try again") self._users.update({ username:age }) return username def get_user(self, username:str) -> None: if username in self._users: return self._users.get(username) else: raise KeyError("no key found for that user") def delete_user(self, username) -> None: if username not in self._users: raise KeyError("no key found for that user") else: del self._users[username] class DuplicateUserError(Exception): def __init__(self, *args): super().__init__(*args) user_generator_exp = ((name, age) for name, age in [ ("luffy", 16), ("luffy", 18), ("fifth emperor", 19) ]) db = UserDatabase() for name, age in user_generator_exp: try: db.add_user(name, age) except ValueError as e: print(f"Got a value error as - {e}") except TypeError as e: print(f"Got a type error as - {e}") except DuplicateUserError as e: print(f"Got a duplicate user error as - {e}") else: print(f"added user {name}") finally: print("block executed")