Write a class PasswordManager
that holds a private dictionary of usernames and passwords. Implement methods to add and change passwords, ensuring that they meet specific criteria (length, characters, etc.). The password must be at least 8 characters long and contain at least one digit.
Example 1:
manager = PasswordManager() manager.add_user("alice", "password123") print(manager.get_password("alice")) Output: "password123"
Example 2:
manager = PasswordManager() manager.add_user("bob", "short") print(manager.get_password("bob")) Output: None
Implement checks to make sure that the password meets the specified criteria before adding or updating it in the dictionary.
class PasswordManager: def __init__(self): self._users = {} def add_user(self, username, password): if len(password) >= 8 and any(char.isdigit() for char in password): self._users[username] = password def get_password(self, username): return self._users.get(username) # Test the class manager = PasswordManager() manager.add_user("alice", "password123") print(manager.get_password("alice")) # Output: "password123"
Unlock AI & Data Science treasures. Log in!