【问题标题】:Is there any reason Python would skip a line?Python有什么理由会跳过一行吗?
【发布时间】:2019-07-21 23:15:36
【问题描述】:

我刚刚学习 python 几个月,我正在尝试编写一个有助于测试密码特征的程序。我很接近得到我需要的东西,但似乎有一行被跳过,我不知道为什么......这是代码:

def main():

    print("Create a password. Password must follow these rules:")
    print("  - password must be at least 8 characters long")
    print("  - password must have one uppercase AND lowercase letter")
    print("  - password must have at least one digit")

    isValidPassword()

def isValidPassword():
    password = []
    password2 = []

    print()

    print("Enter password:", end="")
    pass1 = input("")    
    print("Re-enter password:", end="")
    pass2 = input("")

    password.append(pass1)
    password2.append(pass2)

    if password == password2 and len(password) >= 8 and password.isupper() == False and password.islower() == False and password.isalpha() == False and password.isdigit() == False:
        print("Password will work.")
    else:
        print("Password will not work. Try again.")
        isValidPassword()

main()

当我运行代码时,我的 if 语句下面的打印语句(“密码将起作用。”)不会打印,即使我输入了一个满足所有要求的密码。我已经在 def isValidPassword() 函数之外的另一个文件中运行了 if 语句,它似乎工作得很好。

谁能告诉我为什么这行不通..?

【问题讨论】:

  • 什么是您希望操纵"Password will work"的示例输入
  • 'Bologna3' 和 'Alphab3t' 是示例。大写、小写、数字、字母、8位以上字符……等

标签: python-3.x function if-statement


【解决方案1】:

我认为主要问题在于这里的比较:password == password2,因为您正在测试两个 list 对象是否彼此相等。您应该做的是将输入存储为字符串并测试字符串是否相等。

此代码应按预期工作:

def isValidPassword():
    print("Enter password:", end="")
    password = input("")    
    print("Re-enter password:", end="")
    password2 = input("")

    if password == password2 and len(password) >= 8 and not password.isupper() and not password.islower() and not password.isalpha() and not password.isdigit():
        print("Password will work.")
    else:
        print("Password will not work. Try again.")
        isValidPassword()

【讨论】:

    猜你喜欢
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多