前言
编程就如逆水行舟——不进则退,希望小伙伴们都能坚持练习!
题目要求
1.长度大于8位(必须) +1分
2.包含大、小写字母、数字、其他符号,以上四种至少有三种 +1分
3.不能有长度超过或等于3的字串重复 +1分
4.密码的评级等级默认2分,以上条件满足一条加1分,即最高分5分
5.返回密码的评分等级,及要改进的点,如:return 3,[‘长度小于8’,‘密码没有由3中以上字符组成’]
代码
passwd = input("请输入密码:")
def check_password(password):
score = 2
issues = []
# Check length
if len(password) > 8:
score += 1
else:
issues.append('长度小于8')
# Check character types
types = [0, 0, 0, 0] # uppercase, lowercase, digit, symbol
for char in password:
if char.isupper():
types[0] = 1
elif char.islower():
types[1] = 1
elif char.isdigit():
types[2] = 1
else:
types[3] = 1
if sum(types) >= 3:
score += 1
else:
issues.append('密码没有由3种以上字符组成')
# Check substring repetition
for i in range(len(password) - 2):
substr = password[i:i + 3]
if password.count(substr) > 1:
issues.append('有重复的子串')
break
# Return score and issues
return score, issues
score, issues = check_password(passwd)
print(score, issues)