簡體   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