Basic Password Validation

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

IntermediatePhase 1 - Level 4: Logical Operators & Compound StatementsExample 10 of 10
basic-password-validation.py
1# Take password
2password = input("Enter password: ")
3
4# Check rules
5has_length = len(password) >= 8
6has_digit = any(char.isdigit() for char in password)
7
8if has_length and has_digit:
9 print("Password is valid")
10else:
11 print("Password is invalid")
12 if not has_length:
13 print("- Must be at least 8 characters")
14 if not has_digit:
15 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

What's going on

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