09-检测密码强度

  • 阅读: 312
  • 更新: 2022-06-11

难度:

1. 题目描述

2. 关键点

  • for 循环
  • if 判断

3. 代码实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def password_score(password):
    score = 0

    if len(password) < 6:
        return score

    score += 1
    has_sz = has_xx = has_dx = has_ts = False

    for c in password:
        if has_sz is False and c in '1234567890':
            score += 1
            has_sz = True
        if has_xx is False and c in 'abcdefghijklmnopqrstuvwxyz':
            score += 1
            has_xx = True
        if has_dx is False and c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
            score += 1
            has_dx = True
        if has_ts is False and c in '~!@#$%^&*()_+':
            score += 1
            has_ts = True

    score += len(set(password)) // 6
    return score


if __name__ == '__main__':
    password = input('请输入密码:')
    score = password_score(password)
    if score == 0:
        print('密码长度必须大于等于6位')
    elif score <= 2:
        print('密码强度弱')
    elif score <= 4:
        print('密码强度较强')
    elif score <= 6:
        print('密码强度强')
    else:
        print('密码强度超强')

4. 运行示例

1
2
请输入密码:sdfja2HDH%!d232
密码强度强

5. 进阶思考

  1. 使用 string 标准库来代替数字和小写字母
  2. 编写生成随机密码函数

=== 全文完 ===


欢迎加入QQ群:778391413

京公网安备 11011302003970号 京ICP备2022012301号-1