要使密碼更強大,它需要滿足以下四個規(guī)則:
- . 密碼必須包含超過8個字符,且不超過16個字符。“
提示:查找密碼字符串的長度,您應(yīng)該使用len()方法。
- 密碼必須包含至少一個數(shù)字、一個大寫字母和一個小寫字母。
- 密碼必須包含多個非字母數(shù)字字符。
第一種方式是使用函數(shù)文章來源:http://www.zghlxwxcb.cn/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")
第二種最簡單的方法文章來源地址http://www.zghlxwxcb.cn/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()
到了這里,關(guān)于Python檢測密碼是否符合要求,大小寫字母數(shù)字長度等密碼必須包含超過8個字符,且不超過16個字符。密碼必須包含至少一個數(shù)字、一個大寫字母和一個小寫字母。密碼必須包含多個非字母數(shù)字字符。的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!