I'm a beginner in Python and I made a password checker. I'm following these rules:
- I have to use regex
- password must be at least 8 characters
- must have at least one lower case character
- must have at least one upper case character
- must have at least one special character
- must have at least one number
I also wanted to display a message letting the user know what conditions weren't met.
I'm a bit concerned by the succession of if
s.
#strong password checks
import re
#ask the user for the password to check
pwtocheck = input('please enter the password to be checked:')
#Error message to be
Err_mess = ''
#first regex : at least one lower case characters
pw_lc = re.compile(r'([a-z]+)')
#second regex : at least one upper case characters
pw_uc = re.compile(r'([A-Z]+)')
#third regex: at least one special character
pw_sc = re.compile(r'([\-_\?!@#$%^&\*]+)')
#fourth regex : password must have at least one number
pw_d = re.compile(r'([0-9])+')
#create a list to be given to all() to evaluate if all conditions are met or not
cond_pw = [len(pwtocheck) > 8, pw_lc.search(pwtocheck), pw_uc.search(pwtocheck), pw_sc.search(pwtocheck), pw_d.search(pwtocheck)]
#building an error message
#is the password at least 8 characters
if len(pwtocheck) < 8:
Err_mess = Err_mess + 'password is too short\n'
#first regex is none
if pw_lc.search(pwtocheck) is None:
Err_mess = Err_mess + 'password needs at least one lower case character\n'
#second regex is None
if pw_uc.search(pwtocheck) is None:
Err_mess = Err_mess + 'password needs at least one upper case character\n'
#third regex is None
if pw_sc.search(pwtocheck) is None:
Err_mess = Err_mess + 'password needs at least one special character\n'
#fourth regex is none
if pw_d.search(pwtocheck) is None:
Err_mess = Err_mess + 'password needs at least one number\n'
if all (cond_pw) is True:
print('password is ok')
else:
print(Err_mess)