I'm struggling to get my password checker to recognize my special characters as true when they are inputted, any help would be appreciated!
import re
def password_cri():
while True:
password = input("Please enter your password:")
if len(password)<5 and len(password)>15:
print("Password denied: must be between 5 and 15 characters long.")
elif re.search('[0-9]',password) is None:
print("Password denied: must contain a number between 0 and 9")
elif re.search('[A-Z]',password) is None:
print("Password denied: must contain a capital letter.")
elif re.search('[a-z]',password) is None:
print("Password denied: must contain a lowercase letter.")
elif re.search('[!, @, #, $, %, &, (, ), -, _, [, ], {, }, ;, :, ", ., /, <, >, ?]', password) is None:
print("Password denied: must contain a special character")
else:
print("Your password has been accepted.")
break
password_cri()
CodePudding user response:
'[!, @, #, $, %, &, (, ), -, _, [, ], {, }, ;, :, ", ., /, <, >, ?]'
is probably not the regex you're looking for. Try
'[!@#$%&()\-_[\]{};:"./<>?]'
and note that -
and ]
are escaped because of how the []
regex block works.
CodePudding user response:
That's not the proper regular expression for matching those characters.
I wouldn't actually recommend using a regular expression for this, since your matches are only one character long. Instead, make a string containing all the special characters you want to match, and then use any()
and map()
to determine if any of the special characters appear in the password.
password = "????????"
special_characters = '!@#$%&()-_[]{};:"./<>?'
if any(map(lambda x: x in password, special_characters)):
print("Password contains special characters")
else:
print("Password denied: must contain a special character")