Python是否有任何原因会跳过一行? [英] Is there any reason Python would skip a line?

查看:146
本文介绍了Python是否有任何原因会跳过一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm just a few months into learning python and I'm trying to write a program which will help to test characteristics of a password. I'm so close to getting what I need but one line seems like it keeps getting skipped and I can't figure out why... Here's the code:

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语句下的打印语句("Password will work.")也不会打印.我已经在def isValidPassword()函数之外的另一个文件中运行了if语句,它似乎可以正常工作.

When I run the code, the print statement ("Password will work.") underneath my if statement will not print, even though I enter a password which meets all of the requirements. I have run the if statement in another file, outside of the def isValidPassword() function and it seems to work just fine.

任何人都可以向我提供任何有关为什么这种方法行不通的见解吗??

Can anybody lend me any insight as to why this won't work..?

推荐答案

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

I think the main issue is with the comparisons here: password == password2 as you are testing whether two list objects are equal to each other. What you should be doing is storing the input as strings and test whether the strings are equal.

此代码应可以正常工作:

This code should work as expected:

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()

这篇关于Python是否有任何原因会跳过一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆