赞
踩
要使密码更强大,它需要满足以下四个规则:
提示:查找密码字符串的长度,您应该使用len()方法。
第一种方式是使用函数
- if len(a) > 16 or len(a) < 8:
- print(a + "is not of valid length " + str(len(a)))
- isOk = 0
- else:
- print(a + "is of valid length " + str(len(a)))
- print("Check rule 2")
- # Set the function
- lowercases = 0
- uppercases = 0
- digits = 0
- alpha = 0
- # Find, detect output upper and lower case alphanumeric
- for char in a:
- if char.islower():
- lowercases += 1
- elif char.isupper():
- uppercases += 1
- elif char.isdigit():
- digits += 1
- print("Number of lowercase is: " + str(lowercases))
- print("Number of uppercase is: " + str(uppercases))
- print("Number of digit is: " + str(digits))
- if lowercases == 0 or uppercases == 0 or digits == 0:
- print(a + " does not contain enough of each type of character")
- isOk = 0
- else:
- print(a + " contain enough of each type of character")
- print("check rule 3")
- # Detecting special characters
- if alpha != 0:
- print(a + " contain non-alpha-numeric")
- else:
- print(a + " does not contain non-alphanumeric")
- isOk = 0
- print("Check for all the rules")
- if isOk:
- print("Overall " + a + " isvalid")
第二种最简单的方法
- def password_check(passwd):
- SpecialSym = ['$', '@', '#', '%', '@', '!', '&', '=', '+', '_', '-', '$', '/', ]
- val = True
- # check the length
- if len(passwd) < 8:
- print('length should be at > 8')
- val = False
- else:
- print('length is than 8 ')
-
- if len(passwd) > 16:
- print('length should be not be least than 16')
- val = False
- else:
- print('length is least than 16')
- # check the number
- if not any(char.isdigit() for char in passwd):
- print('Password should have at least one numeral')
- val = False
- else:
- print('password have least one numeral')
-
- # check the uppercase
- if not any(char.isupper() for char in passwd):
- print('Password should have at least one uppercase letter')
- val = False
- else:
- print('Password have at least one uppercase letter')
- # check the lowercase
- if not any(char.islower() for char in passwd):
- print('Password should have at least one lowercase letter')
- val = False
- else:
- print('Password have least one lowercase letter')
- # check the Special characters
- if not any(char in SpecialSym for char in passwd):
- print('Password should have at least one of the symbols $@#')
- val = False
- else:
- print('Password have least one of the symbols $@#')
- if val:
- return val
-
-
- def main():
-
- if password_check(password):
-
- print('Password is valid')
- else:
- print("Invalid Password !!")
-
-
- # Driver Code
- if __name__ == '__main__':
- password = input('Enter your password: ')
- main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。