Write a Python program that asks the user for a password and raises an exception if the password does not meet the criteria of having at least one letter, one number, and being at least 8 characters long.
Example 1:
Input: "12345678" Output: "Error: Password should contain at least one letter and one number."
Example 2:
Input: "password1" Output: "Valid password."
Make use of Python’s built-in string methods like isalpha()
, isdigit()
, and len()
.
def validate_password(password): if len(password) < 8 or not any(char.isdigit() for char in password) or not any(char.isalpha() for char in password): raise Exception("Error: Password should contain at least one letter, one number, and be at least 8 characters long.") else: return "Valid password." print(validate_password("12345678")) # Output: "Error: Password should contain at least one letter, one number, and be at least 8 characters long." print(validate_password("password1")) # Output: "Valid password."
Unlock AI & Data Science treasures. Log in!