密码强度检测规则:

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

主要知识点

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

代码部分

密码强度检测

1、首先创建一个 python 文件

导入系统包

import platform

密码强度检测规则

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

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

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、运行结果

请输入待检测密码: 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
密码符合规则, 检查通过

全部代码

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("密码校验未通过, 请重新输入")
原文地址:https://blog.csdn.net/ooowwq/article/details/125747073

相关文章:

  • 2022-12-23
  • 2022-01-23
  • 2022-01-16
  • 2022-12-23
  • 2022-01-26
  • 2022-12-23
  • 2022-02-11
  • 2021-12-02
猜你喜欢
  • 2022-03-05
  • 2021-12-20
  • 2021-09-11
  • 2022-12-23
  • 2021-12-09
  • 2022-02-17
  • 2021-12-15
相关资源
相似解决方案