I am new to Python and I am trying to create a program to validate passwords using a for loop (I have written one using a while loop already. However it is not working correctly.
Like I said I am very new to Python and code writing. I am finding especially difficult looping through the values of a string (password entered) and resolving how to deal with exceptions (for example, not containing certain characters) This is the code I have written.
import string
import re
print('At least 1 letter between [a-z] and 1 letter between [A-Z]. \nAt least 1 number between [0-9]. \nAt least 1 character from [$#@]. \nMinimum length 6 characters. \nMaximum length 16 characters. \n')
def validate():
password = input('Enter your password: \n')
if (len(password) < 8):
print('Password must be at least 8 characters long')
elif (len(password) >= 16):
print('Password must be no more than 16 characteres long')
for i in password:
if (i.find(string.ascii_lowercase)):
print('At least one character must be in the range [a-z]')
elif (i.find(string.ascii_uppercase)):
print('At least one character must be in the range [A-Z]')
elif (i.find(string.digits)):
print('At least one character must be in range[0-9]')
elif (re.search('[@,#,$]', password) is None):
print('At least one of these characters (@ - # -$) must be included')
else:
print('your password is good:')
break
validate()
[a-z]+ [A-Z]+ [0-9]+ [@#$]+