33def passwordValidator ():
44 """
55 Validates passwords to match specific rules
6- : return: str
6+ :return: str
77 """
8- # display rules that a password must conform to
98 print ('\n Your password should: ' )
109 print ('\t - Have a minimum length of 6;' )
1110 print ('\t - Have a maximum length of 12;' )
12- print ('\t - Contain at least an uppercase letter or a lowercase letter' )
11+ print ('\t - Contain at least an uppercase letter;' )
12+ print ('\t - Contain at least a lowercase letter;' )
1313 print ('\t - Contain at least a number;' )
1414 print ('\t - Contain at least a special character (such as @,+,£,$,%,*^,etc);' )
1515 print ('\t - Not contain space(s).' )
16- # get user's password
16+
1717 userPassword = input ('\n Enter a valid password: ' ).strip ()
18- # check if user's password conforms
19- # to the rules above
18+
19+ # Length check
2020 if not (6 <= len (userPassword ) <= 12 ):
21- message = 'Invalid Password..your password should have a minimum '
22- message += 'length of 6 and a maximum length of 12'
23- return message
21+ return 'Invalid Password..length must be between 6 and 12 '
22+
23+ # No spaces allowed
2424 if ' ' in userPassword :
25- message = 'Invalid Password..your password shouldn\' t contain space(s)'
26- return message
27- if not any (i in string .ascii_letters for i in userPassword ):
28- message = 'Invalid Password..your password should contain at least '
29- message += 'an uppercase letter and a lowercase letter'
30- return message
31- if not any (i in string .digits for i in userPassword ):
32- message = 'Invalid Password..your password should contain at least a number'
33- return message
34- if not any (i in string .punctuation for i in userPassword ):
35- message = 'Invalid Password..your password should contain at least a special character'
36- return message
37- else :
38- return 'Valid Password!'
25+ return 'Invalid Password..shouldn\' t contain spaces'
26+
27+ # Uppercase letter check
28+ if not any (i .isupper () for i in userPassword ):
29+ return 'Invalid Password..should contain at least one uppercase letter'
30+
31+ # Lowercase letter check
32+ if not any (i .islower () for i in userPassword ):
33+ return 'Invalid Password..should contain at least one lowercase letter'
34+
35+ # Number check
36+ if not any (i .isdigit () for i in userPassword ):
37+ return 'Invalid Password..should contain at least one number'
38+
39+ # Special character check
40+ if not any (i in string .punctuation for i in userPassword ):
41+ return 'Invalid Password..should contain at least one special character'
42+
43+ return 'Valid Password!'
3944
4045my_password = passwordValidator ()
41- print (my_password )
46+ print (my_password )
0 commit comments