Skip to content

script_3.py - Password Strength Checker

Code

Python
#!/usr/bin/env python3
"""Password strength checker"""

password = input("Enter password: ")

if len(password) < 6:
    print("Password too short")
elif len(password) > 20:
    print("Password too long")
else:
    print("Password length acceptable")

Explanation

Line 4: password = input("Enter password: ") - input() returns a string type (str) - No type conversion needed - password remains as string - password variable stores the raw user input including spaces and special characters

Line 6: if len(password) < 6: - len() is a built-in function that returns an integer representing string length - Counts the number of characters in the string - < is the less-than comparison operator - Checks if password has fewer than 6 characters

Line 8: elif len(password) > 20: - > is the greater-than comparison operator - Only checked if the first condition (line 6) was False - Checks if password exceeds 20 characters

Line 10: else: - Executes when password length is between 6 and 20 (inclusive) - Catches the "acceptable" range after rejecting too short or too long