要使密码更强大,它需要满足以下四个规则:
- . 密码必须包含超过8个字符,且不超过16个字符。“
提示:查找密码字符串的长度,您应该使用len()方法。
- 密码必须包含至少一个数字、一个大写字母和一个小写字母。
- 密码必须包含多个非字母数字字符。
第一种方式是使用函数文章来源:https://www.toymoban.com/news/detail-448593.html
-
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")
第二种最简单的方法文章来源地址https://www.toymoban.com/news/detail-448593.html
-
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()
到了这里,关于Python检测密码是否符合要求,大小写字母数字长度等密码必须包含超过8个字符,且不超过16个字符。密码必须包含至少一个数字、一个大写字母和一个小写字母。密码必须包含多个非字母数字字符。的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!