密码强度检测器


我的CSDN主页

   python“每日一练”

  1. 题目
  2. 代码运行效果
  3. 完整代码
  4. 我的博文推荐

练习题目

   定义一个名为“isStrongPassword”的函数,该函数将字符串作为参数,功能是将检查所提供的字符串是否满足以下条件,以检查是否为强密码:

密码强度检测器_第1张图片

  该函数将返回一个布尔值,即如果满足所有条件将返回True,否则返回False。确保使用可能返回False值的每个可能的输入来测试函数也一样。

回首页

代码运行效果

密码强度检测器_第2张图片

密码强度检测器_第3张图片

密码强度检测器_第4张图片

密码强度检测器_第5张图片


回首页

python完整代码

(如果从语句注释不能清楚作用,请评论区留言指教和探讨。)
#/sur/bin/nve python
# coding: utf-8


#fileName = 'isstrongpassword.py'
def isStrongPassword(s):
    '''密码强度测验'''
    #判断太多,我使用短语句,便于调试查bug
    #判断大小写字母分别不小于1
    lower = len([i for i in s if i.islower()])
    supper = len([i for i in s if i.isupper()])
    if lower>=1 and supper>=1:
        alpha = True
    else:
        alpha = False
    #判断数字不小于3,数字也可以用re提取。我这里是写的函数,觉得用str.isdigit()更好,不用加载re模块。
    if len([i for i in s if i.isdigit()])>=3:
        digit = True
    else:
        digit = False
    #字母和数字的判断,也可以用not in [0~9,[a~z,A~Z]],我选择了用str.is函数,只是我的习惯,二者等价。
    #判断特殊字符不小于3(特殊字符为除字母数字之外的所有字符)
    symbol = len([i for i in s if not (i.islower() or i.isupper() or i.isdigit())])
    if symbol>=3:
        symbol_b = True
    else:
        symbol_b = False
    #判断字符串长度>=12
    if len(s)>=12:
        s_long = True
    else:
        s_long = False
    #下一行为调试打印语句,可以略去
    print(f'\n密码字符判断:\nAlpha={alpha},Digit={digit},Symbol={symbol>=3},密码长度={s_long}')
    if alpha and digit and symbol_b and s_long:
        return True
    else:
        return False


print(f'\n\n{"【密码强度检测器】".rjust(22)}\n\n')
s = input(f'输入密码字符串:\n$>>')
print(f'\n\n密码字符:{s}\n{"﹊"*21}\n{"密码强度:".rjust(16)}{isStrongPassword(s)}\n{"﹊"*21}\n\n')

回首页

我的博文推荐:

  • 尼姆游戏(聪明版/傻瓜式•人机对战)(当前阅读2626)(代码优化版)
推荐条件: 点阅破千

参考文章:


上一篇: 练习:求列表平衡点
下一篇:


回首页

老齐漫画头像

精品文章:

  • 通过内置对象理解python
  • Python 完全自学手册
  • 海象运算符
  • Python中的 `!=`与`is not`不同
  • 学习编程的正确方法

来源:老齐教室


你可能感兴趣的:(笔记)