题解 | 密码验证合格程序

def check_password(s):
    # 检查长度
    if len(s) <= 8:
        return 'NG'

    # 检查是否包含至少三种不同类型的字符
    has_digit = any(c.isdigit() for c in s)
    has_lower = any(c.islower() for c in s)
    has_upper = any(c.isupper() for c in s)
    has_symbol = any(not c.isalnum() and not c.isspace() for c in s)

    if sum([has_digit, has_lower, has_upper, has_symbol]) < 3:
        return "NG"

    # 检查是否有长度大于2的重复子串
    for i in range(len(s) - 2):
        x = s[i:i+3]
        if x in s[i+3:]:
            return "NG"

    return "OK"

while True:
    try:
        # 提示用户输入密码
        password = input()
        print(check_password(password))
    except KeyboardInterrupt:  # 捕获中断异常,而不是所有异常       
        break
    except Exception as e:
        break

全部评论

相关推荐

12-07 21:21
东北大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务