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()
解决方案
问题在于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."
上面应该找到密码并使用您的逻辑。但是,您不应该使用特定的用户名作为密码吗?这是检查用户名和密码是否可用,而不是密码是否与用户名匹配。当您分别遍历密码和用户名时,您只需检查是否有人有密码和有人有用户名,是否是同一个人。如果是这样,请替换为:
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 == j:
return "Access granted!"
else:
return "Incorrect Login credentials, please try again."
确保用户名和密码在两个列表中的顺序相同。或者,我建议存储一个看起来像这样的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.")
推荐阅读
- android - 在从 ViewModel/LiveData 首次更新之前,Android 中的格式化字符串无法正确显示
- sql - Where、Having 和 OR 子句
- python - 递归神经网络
- sql - 提高 SQL 查询的性能
- sql - SQL查询所有注册但仅选择一列之一
- php - 当我在 Laravel 上创建新行时出现非法偏移类型
- javascript - 本地捕获的“抛出”异常 - 代码重复或警告
- python - 如何从嵌套循环中生成的数据创建热图/熊猫数据框?
- python - 具有动态数量的动态形状张量的 tf.function
- python - 将异常堆栈跟踪从 C++ 传递到 Python(通过 Cython)