题解 | 密码强度等级
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
# 思路:难点---如何计算得分
s = input() # 输入密码
sc = 0 # 总分
# 1.长度
if len(s) >= 8:
sc += 25
elif len(s) >4:
sc += 10
else:
sc += 5
# 2.字母得分
lower_a = [] # 存放小写字母
upper_a = [] # 存放大写字母
for i in s:
if i.islower():
lower_a.append(i)
if i.isupper():
upper_a.append(i)
# 判断得分
if len(lower_a) > 0 and len(upper_a) > 0:
sc += 20
if (len(lower_a) > 0 and len(upper_a) == 0) or (len(lower_a) == 0 and len(upper_a) > 0):
sc += 10
# 3.数字
n = '0123456789'
cnt_num = 0
for i in s:
if i in n:
cnt_num += 1
# 判断得分
if cnt_num == 1:
sc += 10
elif cnt_num > 1:
sc += 20
# 4.符号
str_label = "!#$%&'()*+,-./:;<=\">?@[\]^_`{|}~"
cnt_lab = 0
for i in s:
if i in str_label:
cnt_lab += 1
# 判断得分
if cnt_lab == 1:
sc += 10
elif cnt_lab > 1:
sc += 25
# 5.奖励
if (len(lower_a) > 0 and len(upper_a) > 0) and cnt_num > 0 and cnt_lab > 0:
sc += 5
elif (len(lower_a) > 0 or len(upper_a) > 0) and cnt_num > 0 and cnt_lab > 0:
sc += 3
elif (len(lower_a) > 0 or len(upper_a) > 0) and cnt_num > 0 and cnt_lab == 0:
sc += 2
# 输出评分等级
if sc >= 90:
print("VERY_SECURE")
elif sc >= 80:
print("SECURE")
elif sc >= 70:
print("VERY_STRONG")
elif sc >= 60:
print("STRONG")
elif sc >= 50:
print("AVERAGE")
elif sc >= 25:
print("WEAK")
else:
print("VERY_WEAK")
查看12道真题和解析