[英]Strong password checker
我正在创建一个简单的代码来检查密码是否安全。 为安全起见,密码至少应包含 8 个字符并包含一个特殊字符。 当输入 8 个字符而没有特殊字符时,我的代码无法正常工作。 我该如何解决?
#get password
password_in = input("Enter a secure password: ")
#check password is secure
special_ch = ['!', '@', '#', '$', '%', '^', '&', '*']
check = any(item in special_ch for item in password_in)
while len(password_in) <8 and check == False:
print("Password is not secure")
password_in = input("Please enter a secure password: ")
将and
更改为or
:
while len(password_in) <8 or check is False:
...
或者,要获得相同的结果,请输入您正在寻找的确切条件并否定所有条件:
while not(len(password_in) >= 8 and check is True):
...
这两者在逻辑上等价的事实是德摩根定律的一个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.