Basic Password Validation

Take a password string and check basic rules (length >= 8 and contains at least one digit).

Logic BuildingIntermediate
Logic Building
# Take password
password = input("Enter password: ")

# Check rules
has_length = len(password) >= 8
has_digit = any(char.isdigit() for char in password)

if has_length and has_digit:
    print("Password is valid")
else:
    print("Password is invalid")
    if not has_length:
        print("- Must be at least 8 characters")
    if not has_digit:
        print("- Must contain at least one digit")

Output

Enter password: password123
Password is valid

Enter password: pass
Password is invalid
- Must be at least 8 characters
- Must contain at least one digit

Check multiple password requirements.

Key Concepts:

  • Check length >= 8
  • Check if contains digit using any()
  • Use AND to ensure both conditions
  • Provide specific error messages