我正在创建一个简单的代码来检查密码是否安全。为安全起见,密码至少要有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: ")发布于 2019-10-15 20:52:09
将and更改为or
while len(password_in) <8 or check is False:
...或者,为了达到同样的结果,加入你想要的确切条件,并将其全部否定:
while not(len(password_in) >= 8 and check is True):
...这两个在逻辑上等价的事实是DeMorgan's Law的一个例子。
https://stackoverflow.com/questions/58395090
复制相似问题