题解 | #密码强度等级#
密码强度等级
http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
用函数判断密码长度、是否包含大小写字母、是否包含数字、是否包含符号并返回对应分值,再写一个评判函数。避免直接判断然乱思路。
def length(n):
if len(n) <= 4:
return 5
elif 5 <= len(n) <=7:
return 10
elif 8 <= len(n):
return 25
def alpha(n):
alpha_list = []
upper_list = []
lower_list = []
for i in n:
if i.isalpha():
alpha_list.append(i)
if i.islower():
lower_list.append(i)
elif i.isupper():
upper_list.append(i)
if len(alpha_list) == 0:
return 0
if len(lower_list) != 0 and len(upper_list) == 0:
return 10
if len(lower_list) == 0 and len(upper_list) != 0:
return 10
if length(upper_list) != 0 and len(lower_list) != 0:
return 20
def number(n):
num_list = []
for i in n:
if i.isdigit():
num_list.append(i)
if len(num_list) == 0:
return 0
if len(num_list) == 1:
return 10
if len(num_list) > 1:
return 20
def symbol(n):
symbol_list = []
st = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
for i in n:
if i in st:
symbol_list.append(i)
if len(symbol_list) == 0:
return 0
if len(symbol_list) == 1:
return 10
if length(symbol_list) > 1:
return 25
def reward(n):
if alpha(n) != 0 and number(n) != 0 and symbol(n) == 0:
return 2
if alpha(n) == 10 and number(n) != 0 and symbol(n) != 0:
return 3
if alpha(n) >= 20 and number(n) > 0 and symbol(n) > 0:
return 5
else:
return 0
def score(n):
total = length(n) + alpha(n) + number(n) + symbol(n) + reward(n)
if total >= 90:
print('VERY_SECURE')
elif 80 <= total < 90:
print('SECURE')
elif 70 <= total < 80:
print('VERY_STRONG')
elif 60 <= total < 70:
print('STRONG')
elif 50 <= total < 60:
print('AVERAGE')
elif 25 <= total < 50:
print('WEAK')
else:
print('VERY_WEAK')
while 1:
try:
key = input()
score(key)
except:
break