Write a Python program that simulates a simple authentication system. Raise custom exceptions for various scenarios like user not found, incorrect password, account locked, etc.
Example 1:
Input: Users: {"user1": {"password": "pass1", "locked": False}, "user2": {"password": "pass2", "locked": True}}, Username: "user1", Password: "pass1"
Output: "Authentication successful."
Example 2:
Input: Users: {"user1": {"password": "pass1", "locked": False}, "user2": {"password": "pass2", "locked": True}}, Username: "user1", Password: "wrongpass"
Output: "Error: Incorrect password."
Use a dictionary to store the users and their information.
def authenticate(users, username, password):
if username not in users:
raise Exception("Error: User not found.")
elif users[username]["locked"]:
raise Exception("Error: Account locked.")
elif users[username]["password"] != password:
raise Exception("Error: Incorrect password.")
else:
return "Authentication successful."
users = {"user1": {"password": "pass1", "locked": False}, "user2": {"password": "pass2", "locked": True}}
print(authenticate(users, "user1", "pass1")) # Output: "Authentication successful."
print(authenticate(users, "user1", "wrongpass")) # Output: "Error: Incorrect password."
Unlock AI & Data Science treasures. Log in!