关键词搜索

源码搜索 ×
×

利用Python检验用户输入密码的复杂度,非常简单

发布2022-07-13浏览1788次

详情内容

今天分享一下用Python检测用户输入密码的复杂度,灰常简单!

密码强度检测规则:

  • 至少包含一个数字
  • 至少包含一个大写字母
  • 长度至少 8 位

主要知识点

  • while 循环
  • 推导式
  • 列表 any 函数
  • 命令行 input

代码部分

密码强度检测

1、首先创建一个 python 文件

导入系统包

import platform

    密码强度检测规则

    1. 至少包含一个数字
    2. 至少包含一个大写字母
    3. 长度至少 8 位

    每天打印一词,激励一下自己。

    print("人生苦短,我用Python")
    

      输入密码

      while True:
          password = input("请输入待检测密码: ")
      
        2

      列表推导式使用

      print("数字检测: ", [i.isdigit() for i in password])
      print("大写字母检测: ", [i.isupper() for i in password])
      print("密码长度: ", len(password))
      
        2
      • 3

      是否有数字, 推导式检测。

      hasNumber = any([i.isdigit() for i in password])
      

        是否有大写字母, 推导式检测。

        hasUpper = any([i.isupper() for i in password])
        

          密码检测

          if hasNumber and hasUpper and len(password) >= 8:
              print("密码符合规则, 检查通过")
              break
          else:
              print("密码校验未通过, 请重新输入")
          
            2
          • 3
          • 4
          • 5

          2、运行结果

          请输入待检测密码: 123213
          数字检测:  [True, True, True, True, True, True]
          大写字母检测:  [False, False, False, False, False, False]
          密码长度:  6
          密码校验未通过, 请重新输入
          请输入待检测密码: abc1234
          数字检测:  [False, False, False, True, True, True, True]
          大写字母检测:  [False, False, False, False, False, False, False]
          密码长度:  7
          密码校验未通过, 请重新输入
          请输入待检测密码: Abc34567
          数字检测:  [False, False, False, True, True, True, True, True]
          大写字母检测:  [True, False, False, False, False, False, False, False]
          密码长度:  8
          密码符合规则, 检查通过
          
            2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15

          全部代码

          import platform
           
          print("人生苦短,我用Python")
           
          while True:
              password = input("请输入待检测密码: ")
           
              print("数字检测: ", [i.isdigit() for i in password])
              print("大写字母检测: ", [i.isupper() for i in password])
              print("密码长度: ", len(password))
           
              hasNumber = any([i.isdigit() for i in password])
           
              hasUpper = any([i.isupper() for i in password])
           
              if hasNumber and hasUpper and len(password) >= 8:
                  print("密码符合规则, 检查通过")
                  break
              else:
                  print("密码校验未通过, 请重新输入")
          
            2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20

          兄弟们,来都来了,点个关注在走呗!

          相关技术文章

          点击QQ咨询
          开通会员
          返回顶部
          ×
          微信扫码支付
          微信扫码支付
          确定支付下载
          请使用微信描二维码支付
          ×

          提示信息

          ×

          选择支付方式

          • 微信支付
          • 支付宝付款
          确定支付下载