题目:用户输入一个字符串做为密码,判断密码强度,规则为:密码长度小于8弱密码,密码长度大于等于8且包含至少2种字符为中等强度、密码包含3种字符为强、包含全部4种字符为极强
参考代码实现如下,望采纳
import re
def check_password_strength(password):
# 密码长度小于8,则为弱密码
if len(password) < 8:
return "Weak"
# 使用正则表达式匹配,查找密码中是否包含数字、小写字母、大写字母、特殊字符
nums = re.findall(r'\d', password)
lowers = re.findall(r'[a-z]', password)
uppers = re.findall(r'[A-Z]', password)
specials = re.findall(r'[^a-zA-Z0-9]', password)
case_nums = len(nums) + len(lowers) + len(uppers) + len(specials)
# 密码包含2种字符,则为中等强度
if case_nums >= 2:
return "Medium"
# 密码包含3种字符,则为强密码
if case_nums >= 3:
return "Strong"
# 密码包含4种字符,则为极强密码
if case_nums >= 4:
return "Very Strong"
# 测试示例
print(check_password_strength("123456")) # 输出:Weak
print(check_password_strength("12345678")) # 输出:Medium
print(check_password_strength("12345678a")) # 输出:Strong
print(check_password_strength("12345678a$")) # 输出:Very Strong
你把key in string.digits
换成key.isdigit()
就不需要引入string类了
此外,你把自己敲的代码放出来看,人家代码能跑是因为缩进正确,你的代码报错保证是没注意缩进
报什么错?