繁体   English   中英

Python:如何比较从 a.txt 读取的两个列表(用户名和密码)与用户输入

[英]Python: how do I compare two lists (username & password) read from a .txt to a user input

我编写了一个代码,应该要求用户输入用户名和密码。 然后程序应该打开一个包含用户名和密码列表的.txt 文件,并检查输入的条目是否对.txt 文件中的用户名和密码数据库有效。 这是我第一次在这里发帖,所以请告知您还需要什么。 我希望附上 my.py 文件和 .txt 文件。

我可以输入第一个用户名和密码,但第二个,我必须输入两次才能成功。 我确定这是由于 for 循环,但是我不知道如何将输入与存储在 .txt 中的用户名和密码进行比较

#======== User Login ====================

#read the use.txt
username_reg = []
password_reg = []
i = 0
username_list = []
password_list = []

with open('user.txt', 'r+') as f:           #open user.txt
    for line in f:                          #Now we reading user.txt
        line = line.replace(" ", "")        #replace space before password with no space
        line = line.replace("\n", "")       #remove the next line character as this will take a space in the list
        line = line.split(",")              #separate user name and password
        username_reg = line[0]
        password_reg = line[1]
        username_list.append(username_reg)
        password_list.append(password_reg)
#List check      
print(username_list)
print(password_list)
print(len(username_list))

username = input("Please enter username: ")
password = input("Please enter password: ")

for i in range(len(username_list)):
    if username == str(username_list[i]) and password == str(password_list[i]):
        print('''Please select one of the following options:
                r - register user
                a - add task
                va - view all tasks
                vm - view my tasks
                e - exit''')
        break

    while username != str(username_list[i]) and password != str(password_list[i]):
        print("invalid username or password")   
        username = input("Please enter username: ")
        password = input("Please enter password: ")
        i += 1

print("Great success") 

我试图稍微修改一下 for 循环,它对我有用。 看一看。

i = 0; login = 0 #Here login is like a flag. Which get the value 1 once the login is successful else stays 0.
while i < len(username_list):
    if username == str(username_list[i]) and password == str(password_list[i]):
        login = 1
        print('''Please select one of the following options:
                r - register user
                a - add task
                va - view all tasks
                vm - view my tasks
                e - exit''')
        break
    i+=1
    if i==len(username_list) and login == 0:
        print("invalid username or password")   
        username = input("Please enter username: ")
        password = input("Please enter password: ")
        i = 0

for 循环中的第二个 if 条件检查列表中的所有值是否登录不成功,这意味着用户名和密码不正确,因此 if 条件要求新的输入并重置 while 循环以重新开始0。

我会以不同的方式处理这个问题。 在将某些特定数据与实体相关联时,字典非常有用。 在这种情况下,我将使用一个将密码与每个用户名相关联。

考虑到您的数据库存储信息如下:

user1, password1
user2, password2
user3, password3

然后我们可以执行以下操作:

content = {}

with open('db.txt', 'r') as file:
    for line in file.read().splitlines():
        user = line.split(', ')
        content.update({user[0]: user[1]})

username = input("Please enter username: ")
password = input("Please enter password: ")

if password == content[username]:
    print('''Please select one of the following options:
                r - register user
                a - add task
                va - view all tasks
                vm - view my tasks
                e - exit''')

我们基本上是在打开文件,读取它的行(通过使用str类的splitlines() function),并遍历每一行,分离用户名和密码(通过使用split(', ')因为 ', '是分隔符)并使用与每个用户对应的新记录更新content字典。

然后,在请求凭据之后,我们只需在content字典中查找给定的用户名并检查密码是否相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM