App 3: Password Generator & Checker / Build the password tool
Lesson 6 of 10
Checking strength
Score a password: points for length and for each character type, minus points for common passwords. Turn the score into Weak, Okay or Strong.
Key points
- Length counts the most
- Check a list of common passwords
- Give a label the user understands
COMMON = {"password", "123456", "qwerty", "letmein", "admin"}
def score(pw):
if pw.lower() in COMMON:
return 0
s = min(len(pw), 20)
s += 5 * sum([any(c.islower() for c in pw), any(c.isupper() for c in pw),
any(c.isdigit() for c in pw), any(not c.isalnum() for c in pw)])
return sYour turn
Write score() and print Weak below 20, Okay below 32 and Strong otherwise.
Quiz · +10 XP
What should a password from the common list score?
Log in to save progress and earn XP.