【发布时间】:2020-05-19 13:27:11
【问题描述】:
我正在尝试编写一个代码来验证您访问程序的密码。密码长度至少为 6 个字符,包含 1 个大写字母、1 个小写字母、1 个数字和 1 个标点符号。这就是我现在所拥有的:
def main():
while True:
password = input("Please enter the desired password: ")
if not password.islower():
print("This is NOT a valid password.")
elif not password.isupper():
print("This is NOT a valid password.")
elif not password.isdigit():
print("This is NOT a valid password.")
elif not len(password) < 6:
print("This is NOT a valid password.")
else:
print("This is a valid password.")
break
main()
由于某种原因,即使我写了一个有效的密码,它也会一直打印密码无效,有谁知道为什么会发生这种情况?此外,如何让代码检测到我写的密码中至少有 1 个标点符号?谢谢!
【问题讨论】:
-
尝试用英语单词准确地解释您希望每个条件完成的内容。例如:
if not password.islower():您希望哪些值符合此条件(因此无效)?您希望哪些值不 匹配此条件(以便检查其他值)?现在,尝试验证这一点。例如,您可以为每个“不是有效密码”行使用不同的消息,以便查看满足哪个条件。 -
将
elif not len(password) < 6:更改为elif len(password) < 6: -
@KarlKnechtel 使用
if not password.islower():我希望它能检测我输入的密码是否小写,然后打印“Not a Valid Password”。 -
仍然需要帮助还是已有有用的答案?
标签: python while-loop passwords