使用列表检查用户名和密码

2024-05-18 17:51:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道为什么这个代码不起作用。我有一个错误TypeError:'int'object is not iterable我需要代码来检查两个不同列表中的用户名和密码,如果用户名和密码与列表中的位置匹配且正确,则用户“被授予访问权限”

#Part 1: Opening the files and grabbing data
filename1 = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\usernames.txt"
file = open(filename1, "r")

#Usernames
users = file.read()
usernameslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\usernames.txt")]
print(users)                #Check file
print(usernameslist)       #Check usernames list

filename2 = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt"
file = open(filename2, "r")

#Passwords
passwords = file.read()
passwordslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt")]
print(passwords)            #Check file
print(passwordslist)       #Check passwords list

#Compile the usernames and passwords lists for easy checking
compiled_list = list(zip(usernameslist,passwordslist))
print(compiled_list)

#Scores
filename3 = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt"
file = open(filename3, "r")

scores = file.read()
scoreslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt")]
print(scores)           #Check file
print(scoreslist)       #Check scores

def login():
    username = input("Please enter your username: ")
    password = input("Please enter your password: ")  
    for i in range(len(usernameslist)):
        if username == i and password == [i]:
            print("Access granted!")
        else:
            print("Incorrect Login credentials, please try again.")


login()

Tags: txtforchecklinescriptsopenusersdocuments
1条回答
网友
1楼 · 发布于 2024-05-18 17:51:45

问题出在login()中,您将i用作自身的迭代器。在

def login():
    username = input("Please enter your username: ")
    password = input("Please enter your password: ")  
    for i in [usernameslist]:
        if username == [i]:
            for j in [passwordslist]:
                if password == [j]:
                    return "Access granted!"
                else:
                    return "Incorrect Login credentials, please try again."
        else:
            return "Incorrect Login credentials, please try again."

上面的代码应该找到密码并使用您的逻辑。 但是,您不应该使用特定的用户名作为密码吗?这是检查用户名和密码是否可用,而不是密码是否与用户名匹配。当您分别遍历password和username时,只需检查某人是否拥有密码和用户名,无论是否是同一个人。如果是,则替换为:

^{pr2}$

确保用户名和密码在两个列表中的顺序相同。或者,我建议存储一个如下所示的2D列表:

list = [["Annie","password1"],["bob","wordpass39"]]其中第一个条目是用户名,第二个条目是密码。在

编辑:使用固定的login()函数,您现在应该执行以下操作:

def login():
    username = input("Please enter your username: ")
    password = input("Please enter your password: ")  
    for i in range(len(usernameslist)):
        if username == i and password == i:
            print("Access granted!")
        else:
            print("Incorrect Login credentials, please try again.")

相关问题 更多 >

    热门问题