3
3
def passwordValidator ():
4
4
"""
5
5
Validates passwords to match specific rules
6
- : return: str
6
+ :return: str
7
7
"""
8
- # display rules that a password must conform to
9
8
print ('\n Your password should: ' )
10
9
print ('\t - Have a minimum length of 6;' )
11
10
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;' )
13
13
print ('\t - Contain at least a number;' )
14
14
print ('\t - Contain at least a special character (such as @,+,£,$,%,*^,etc);' )
15
15
print ('\t - Not contain space(s).' )
16
- # get user's password
16
+
17
17
userPassword = input ('\n Enter a valid password: ' ).strip ()
18
- # check if user's password conforms
19
- # to the rules above
18
+
19
+ # Length check
20
20
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
24
24
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!'
39
44
40
45
my_password = passwordValidator ()
41
- print (my_password )
46
+ print (my_password )
0 commit comments