定义一个名为“ isStrongPassword”的函数,该函数将字符串作为参数。功能然后将检查所提供的字符串是否满足以下条件,以检查是否为强 密码: 1.必须至少包含1个大写和小写字母的组合 2.必须至少包含3位数字 3.必须至少包含3个特殊字符(包括空格) 4.密码长度必须至少12个字符 该函数将返回一个布尔值,即如果满足所有条件则返回True或返回False 除此以外。 确保使用可能返回False值的每个可能的输入来测试函数也一样
def isStrongPassword(pwd):
chars = list(pwd)
upper = [c for c in chars if 'A' <= c and c <= 'Z']
lower = [c for c in chars if 'a' <= c and c <= 'z']
digit = [c for c in chars if '0' <= c and c <= '9' ]
symbol = [c for c in chars if not ('A' <= c and c <= 'Z' or 'a' <= c and c <= 'z' or '0' <= c and c <= '9')]
strong = len(upper) >= 1 and len(lower) >= 1 and len(digit) >= 3 and len(symbol) >= 3 and len(pwd) >= 12
return strong
print(isStrongPassword("Str0n9P@$$w0rd"))
print(isStrongPassword("StrongPassword"))
print(isStrongPassword("Stron9P@$$0rd"))
print(isStrongPassword("Str0n9Pass0rd"))
print(isStrongPassword("str0n9p@$$0rd"))
print(isStrongPassword("Str0n9P@$$"))
print(isStrongPassword("12345678"))
print(isStrongPassword("~!@#$%^&*()_+"))
print(isStrongPassword("STRONGPASSWORD"))
# output
True
False
False
False
False
False
False
False
False
def isStrongPassword(s):
upper=0
lower=0
digit=0
other=0
for c in s:
if c.isupper():
upper+=1
elif c.islower():
lower+=1
elif c.isdigit():
digit+=1
else:
other+=1
return len(s)>=12 and upper>=1 and lower>=1 and digit>=3 and other>=3
if __name__ == "__main__":
print(isStrongPassword("999Aggg-----")) # True
print(isStrongPassword("999Aggg----")) # False
print(isStrongPassword("999aggg-----")) # False
print(isStrongPassword("999AAAA-----")) # False
print(isStrongPassword("99AAggg-----")) # False
print(isStrongPassword("999AAAAggg--")) # False
如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632