输入一个字符串作为密码,密码只能由数字与字母组成。编写程序判断输入的密码的强度,并输出,如图所示。判断标准如下,满足其中一条,密码强度增加一级:①有数字;②有大写字母;③有小写字母;④位数不少于8位。
那就逐个字符判断取值范围吧
s = input("请输入测试密码(直接回车为退出):\n")
while len(s) != 0:
N = 0
B = 0
A = 0
L = 0
if len(s) >= 8:
L = 1
for a in s:
if a >= 'A' and a<='Z':
B = 1
elif a>='a' and a<='z':
A = 1
elif a>='0' and a<='9':
N = 1
print(f"{s}的密码强度为{L+A+B+N}级")
s = input("请输入测试密码(直接回车为退出):\n")
第一个测试用例不对吧,abc123应该2级啊
def checkPwd(pwd):
a = b = c = d = False
for p in pwd:
if p.isdigit(): a = True
elif p.isupper(): b = True
elif p.islower(): c = True
if len(pwd) >= 8: d = True
return a+b+c+d
pwd = input()
res = checkPwd(pwd)
print(f"{pwd}的密码强度为{res}级")
import re
while True:
password=input('请输入测试密码(直接回车为退出):')
#输入会车直接退出
if password == '':
break
# 判断是否只含有字母和数字
isOtherCode=re.compile('[\s\W]+')
# print( re.findall(isOtherCode,password) )
if not re.findall(isOtherCode,password):
N=U=A=L=0
if len(password) >= 8: L=1
for item in password:
if item.isdigit(): N=1
elif item.isupper():U=1
elif item.isalpha(): N=1
print(f'{password} 的密码强度为{N+U+A+L}级')
else:
print('密码中包含除字母和数字外的字符')
思路:
代码实现:
password = input("请输入密码:")
level = 0
if any(char.isdigit() for char in password):
level += 1
if any(char.isupper() for char in password):
level += 1
if any(char.islower() for char in password):
level += 1
if len(password) >= 8:
level += 1
if level == 0:
print("密码强度:弱")
elif level == 1:
print("密码强度:中")
elif level == 2 or level == 3:
print("密码强度:强")
else:
print("密码强度:极强")
示例:
输入:123abc
输出:密码强度:中
输入:Abcdefg123
输出:密码强度:强
输入:Abcdefg123!
输出:密码强度:极强