relatively new to Python, but I haven't seen anything like this on SO just yet.
I am in the process of building a password validator in Python, I have already created several rules such as length, alphanumeric, caps, etc. The portion I am hung up on is that I need a specific special character set to check for as well. I am unsure if I just have a syntax issue, or if I am not building the code check portion, but no matter what I enter, it seems to always flag the special characters as not being in the password even if they are.
I attempted to call out the special character list as follows:
spec= "!@#$%&_="
The portion doing the work is in a series of validation checks for each item (mentioned above), for the special character check I am attempting:
elif not any(s in spec for char in s):
print ("Password must contain a special character of !@#$%&_=")
The program runs through but hits on the special character print statement even with a valid password that should pass. I believe I am getting hung up on the elif + any in statements and the syntax is off.
NOTE: I want to avoid using regular expressions, and use the specified special character list.
Thank you in advance!
Fix you replace any(s in spec for char in s)
by any(char in spec for char in s)
.
To check if password contains at least a special character do:
special_character = "!@#$%&_="
def is_valid(pwd):
global special_character
return any(char in spec for char in pwd)
def is_valid_other_method(pwd):
global special_character
return len(set(special_character).intersection(set(pwd))) > 0
print(is_valid('qwerty!'))
# True
print(is_valid('qwerty'))
# False
Output:
True
False