App 3: Password Generator & Checker / Build the password tool
Lesson 4 of 10
Guarantee every character type
A random password might, by bad luck, have no digit. Keep generating until it has a lowercase letter, an uppercase letter, a digit and a symbol.
Key points
- any() checks if at least one matches
- Loop until all rules pass
- Still random, never weakened
def strong_password(length=16):
while True:
pw = make_password(length)
if (any(c.islower() for c in pw) and any(c.isupper() for c in pw)
and any(c.isdigit() for c in pw) and any(not c.isalnum() for c in pw)):
return pwYour turn
Use strong_password() in your menu instead of make_password().
Quiz · +10 XP
What does any(c.isdigit() for c in pw) check?
Log in to save progress and earn XP.