【发布时间】:2021-07-08 22:58:18
【问题描述】:
我正在尝试用 Python 制作一个简单的密码检查器。 该程序要求用户输入超过 8 个字母/符号和 if/else 语句的密码,如果它不包含大写/小写字母和数字,但每次我输入一些内容时,它都会打印“密码足够强”即使我没有输入大写/小写字母或数字。因此,如果有人可以帮助我,我将不胜感激。
这是代码:
password = input("Input your password: ")
if (len(password)<8):
print("Password isn't strong enough")
elif not ("[a-z]"):
print("Password isn't strong enough")
elif not ("[A-Z]"):
print("Passsword isn't strong enough")
elif not ("[0-9]"):
print("Password isn't strong enough")
else:
print("Password is strong enough")
【问题讨论】:
-
想想
not ("[a-z]")在做什么。首先,括号在这里没有用,所以它只是not "[a-z]"。所以not运算符被应用于一个字符串。由于字符串非空,因此被认为是真的,所以not "[a-z]"的计算结果为False。所以所有的elif语句都等价于elif False:。请注意,它们根本不引用password,因此password仅用于初始长度检查。 -
elif not ("[a-z]")您似乎打算用这段代码来检查密码是否包含任何小写字母,但这不是这段代码实际上在做什么。 -
如果你在 Linux 下运行,你可能想研究一下 PAM(Pluggable Authentication Modules),它是一个复杂的系统,但允许将这些东西抽象到配置文件中。它正在(慢慢地)被X/Open 采纳为标准。
-
用你自己的话说,你觉得
elif not ("[a-z]"):到底是什么规则?按照什么逻辑?
标签: python if-statement passwords