密码验证 - python

password validation - python

我正在尝试编写代码来验证密码。但是它不起作用,关于如何解决这个问题有什么想法吗?

password = input("Please enter password: ")

length = False
digit = False
capital = False

length = len(password)

if length > 6:
    length = True
    #print("Length Good")

for count in password:
    if count.isdigit():
        digit = True
        #print ("Contains digit")

for count in password:
    if count.isupper():
        capital = True
        #print ("Contains a capital")

if length != True and digit != True and capital != True:
    print("Password is good")
else:
    print("Bad password")

关于如何解决这个问题的任何想法,谢谢

我 运行 它并输入“测试”,它说“密码正确”。我怀疑你的意思是:

password = input("Please enter password: ")

length = False
digit = False
capital = False

length = len(password)

if length > 6:
    length = True
    #print("Length Good")

for count in password:
    if count.isdigit():
        digit = True
        #print ("Contains digit")

for count in password:
    if count.isupper():
        capital = True
        #print ("Contains a capital")

if length == True and digit == True and capital == True:
    print("Password is good")
else:
    print("Bad password")

否则您将强制使用错误的密码。

在您的代码中,您将密码长度分配给变量 length,然后立即用 False:

覆盖 length 中的值
length = len(password)
length = False

此外,您要检查 lengthdigitcapital 是否为 True,因为检查它们的 False 会给您相反的结果除非你翻转声明

您应该将 len(password) 移动到支票本身,如下所示:

password = input("Please enter password: ")

length = False
digit = False
capital = False

if len(password) > 6:
    length = True
    # print("Length Good")

for count in password:
    if count.isdigit():
         digit = True
         # print ("Contains digit")
    elif count.isupper():
         capital = True
         # print ("Contains a capital")

if length == True and digit == True and capital == True:
    print("Password is good")
else:
    print("Bad password")

您正在设置

length = len(password)

及之后

length = False

所以Python首先将长度设置为int,并在第二条指令中Python取消变量的值并将长度变量设置为布尔值(False)并且条件长度> 6失败。