我已经编写了一个密码检查程序来检查密码的安全性。有三个功能可以检查安全性并返回值,弱,中等和强。我在这里调用这些函数:
if check_pass_strength_weak(password):
print "Weak"
elif check_pass_strength_medium(password):
print "Medium"
elif check_pass_strength_strong(password):
print "Strong""
但强者永远不会归来。这是功能。
def check_pass_strength_strong(password):
upper_count = 0
lower_count = 0
number_count = 0
for letter in password:
if letter == letter.upper():
upper_count = upper_count + 1
elif letter == letter.lower():
lower_count = lower_count + 1
elif letter.isalpha():
number_count = number_count + 1
if upper_count > 0 and lower_count > 0 and number_count > 0:
return True
答案 0 :(得分:0)
你的问题在于:
'1' == '1'.upper()
所以elif
的其余部分永远不会运行,number_count
永远不会增加。翻转它:
if letter.isdigit():
...
elif ...
或者,更好的是,实际使用Python str
methods,并记住return False
:
def check_pass_strength_strong(password):
upper_count = 0
lower_count = 0
number_count = 0
for letter in password:
if letter.isdigit():
number_count += 1
elif letter.isupper():
upper_count += 1
elif letter.islower():
lower_count += 1
if upper_count > 0 and lower_count > 0 and number_count > 0:
return True
return False
事实上,当您检查多个级别时,您可以将计数分解为:
def count_char_types(password):
lower_count = upper_count = number_count = 0
for letter in password:
if letter.isdigit():
number_count += 1
elif letter.isupper():
upper_count += 1
elif letter.islower():
lower_count += 1
return lower_count, upper_count, number_count
然后实施,例如。
def check_pass_strength_strong(password):
l, u, n = count_char_types(password)
return l > 0 and u > 0 and n > 0
并考虑添加symbol_count
!
答案 1 :(得分:0)
您的代码需要两个修复程序。首先,正如jonrsharpe his answer '1' == '1'.upper()
所述,if letter.isdigit():
#...
elif #upper/lower conditions
所以您不会计算这些数字。您需要先验证这一点:
if check_pass_strength_strong(password):
print "Strong"
elif check_pass_strength_medium(password):
print "Mediu"
elif check_pass_strength_weak(password):
print "Weak"
您可能想要改变密码强度的条件:
{{1}}
答案 2 :(得分:0)
使用正则表达式检查数字/大写/小写字符怎么样?
e.g:
def check_pass_strength_strong(password):
return all((re.findall(r'[A-Z]', password),
re.findall(r'[a-z]', password),
re.findall(r'[0-9]', password)))