import os
import time
def strong(password, verifier):
symbols = "! # $ % & ( ) * , - . / : ; = ? @ [ ] ^ _ ` { | } ~"
password = str(password)
if len(password) > 8:
if len(password) < 15:
for i in symbols:
if password.find(symbols) is True:
I want to see if I can fix this part because I am not sure how I can use this function to find a special character in the password.
if password.isalnum():
verifier = 1
return 'good password', verifier
elif password.isalpha():
return 'Your password is only letters'
elif password.isnumeric():
return 'Your password is only numbers'
else:
return 'no symbols'
else:
return 'Too big'
else:
return 'Too small'
CodePudding user response:
You can use any()
along with a generator comprehension to check this:
if any(ch in password for ch in symbols):
I will point out that the symbols list also contains spaces, so you may want to take those out if you don't want a space to qualify as a symbol.
CodePudding user response:
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
CodePudding user response:
Make your list of special characters a set and you'll be able to use the isdisjoint
function to check that there are no symbols in the password:
if {*"!#$%&()* ,-./:;=?@[]^_`{|}~"}.isdisjoint(password):
print("Password has no symbols")
else:
print("Password contains at least one symbol")