You can simplify this (and make debugging easier) by just return
ing after each failed check instead of having all the deeply nested if/else
. Since in the "good password" case you're returning a "verifier" as well as the "good password" message, I'm figuring you probably want to return a false value for the "bad password" cases as well:
def is_strong(password):
symbols = set("!#$%&()*+,-./:;=?@[]^_`{|}~")
if len(password) < 9:
return "Too small", False
if len(password) > 14:
return "Too big", False
if not any(c in symbols for c in password):
return "No symbols", False
if password.isalpha():
return "Your password is only letters", False
if password.isnumeric():
return "your password is only numbers", False
return "Good password", True
Once the function is simplified down, we can see that the isalpha
and isnumeric
checks won't actually do anything, because we've already established that the password contains non-alphanumeric symbols! Instead I think you want to do:
def is_strong(password):
symbols = set("!#$%&()*+,-./:;=?@[]^_`{|}~")
if len(password) < 9:
return "Too small", False
if len(password) > 14:
return "Too big", False
if not any(c in symbols for c in password):
return "No symbols", False
if not any(c.isalpha() for c in password):
return "No letters", False
if not any(c.isdigit() for c in password):
return "No numbers", False
return "Good password", True
We can test the function like this:
strong, password = False, ""
while not strong:
password = input("Set password: ")
msg, strong = is_strong(password)
print(msg)
Set password: bob
Too small
Set password: bobledeboingerberoo
Too big
Set password: bobbington
No symbols
Set password: bobbington!
No numbers
Set password: bobbington3!
Good password