python“每日一练”
- 题目
- 代码运行效果
- 完整代码
- 我的博文推荐
- 基础更熟代码更优——再炼同类问题(2022-11-27试炼)
练习题目
定义一个名为“isStrongPassword”的函数,该函数将字符串作为参数,功能是将检查所提供的字符串是否满足以下条件,以检查是否为强密码:
该函数将返回一个布尔值,即如果满足所有条件将返回True,否则返回False。确保使用可能返回False值的每个可能的输入来测试函数也一样。
代码运行效果
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')
题目
代码
#!/usr/bin/nve python
# coding: utf-8
import re
passwords = input('\n请输入密码:').strip()
# bool
islong = 10 <= len(passwords) <= 15
a, b, c, d = [re.findall(i, passwords) for i in (r'\d', r'_', r'[a-z]', r'[A-Z]')] # re提取数字、下划线、大小写字母。
# is True
if islong and a and b and c and d:
print(f"\n{' 密码设置成功!':~^43}")
else:
print(f"\n{' 密码不符合要求,请重新设置!':~^36}")
运行效果截图
我的博文推荐:
- 尼姆游戏(聪明版/傻瓜式•人机对战)(当前阅读2626)(代码优化版)
推荐条件:
点阅破千
参考文章:
上一篇: 练习:求列表平衡点
下一篇:
精品文章:
来源:老齐教室