字符串是WEAK密码,如果:长度小于8个字符,或者是英语单词,则函数is_english_word()为True。
字符串是强密码,如果:包含至少11个字符,并且包含至少1个小写字母,并且包含至少1个大写字母,并且包含至少1个数字。
如果字符串不是弱密码并且不是强密码,则它是中密码。
def is_english_word( string ):
with open("english_words.txt") as f:
word_list = []
for line in f.readlines():
word_list.append(line.strip())
if string in word_list:
return True
elif string == string.upper() and string.lower() in word_list:
return True
elif string == string.title() and string.lower() in word_list:
return True
else:
return False
def password_strength( string ):
lower = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
upper = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
for item in string:
if item in lower:
string = string.replace(item, "x")
elif item in upper:
string = string.replace(item, "y")
elif item.isnumeric():
string = string.replace(item, "n")
for item in string:
if len( string ) < 8 or is_english_word( string ) :
return 'WEAK'
elif len( string ) >= 11 and string.count("x") >= 1 and string.count("y") >= 1 and string.count("n") >= 1:
return 'STRONG'
else:
return 'MEDIUM'
print( password_strength( 'Unimaginatively' ) )
此密码应为“弱”,但输出为“中”,我不知道我的代码有什么问题。非常感谢。
您的代码有很多问题;值得注意的是,你与替代小写字符x
,大写y
和以数字n
,调用之前is_english_word
-这意味着is_english_word()
将调用'Xyyyyyyyyyyyyyy'
这不是一个英文单词。那就是使您的密码不正确'WEAK'
。
既然也不是'STRONG'
,它最终就成为了'MEDIUM'
。
作为记录,这是一个正确的代码示例,可用于执行您想要的操作:
import string
def password_strength(string):
if len(string) < 8 or is_english_word(string):
return 'WEAK'
elif (len(string) > 11 and
any(ch in string.ascii_lowercase for ch in string) and
any(ch in string.ascii_uppercase for ch in string) and
any(ch.isdigit() for ch in string)):
return 'STRONG'
else:
return 'MEDIUM'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句