簡體   English   中英

Python 中是否有辦法創建用戶名和密碼菜單,其中用戶名和密碼通過 .txt 文件中的信息進行驗證?

[英]Is there a way in Python to create a user name and password menu where the username and password are validated through information in a .txt file?

對 python 來說還是新手,所以說用戶 id 是正確的,並且它將用戶 id 與密碼匹配,然后允許用戶訪問功能或生成錯誤消息並要求用戶再次輸入詳細信息

下面顯示了如何執行此操作的示例。

# read in username/password data from users.txt
with open('users.txt', 'r') as f:
    users = f.read().splitlines()

# create key/value pairs in dictionary for username/password
logins = {}  # create empty dictionary
for user in users:  # one user at a time
    un = user.split(',')[0]  # get user name
    pw = user.split(',')[1]  # get password
    logins[un] = pw  # add to dictionary

# ask user for info
username = input("Enter username: ")
password = input("Enter password: ")

# keep asking if incorrect user name or incorrect password
while username not in logins.keys() or logins[username] != password:
    if username not in logins.keys():  # if username not in list
        print("Not in user list... Try again.")
    elif logins[username] != password:  # if password does not match
        print(f"Not the correct password for {username}... Try again.")
    # ask user for info again
    username = input("Enter username: ")
    password = input("Enter password: ")

print("Access granted!")
# rest of the program goes below here

用戶名和密碼信息存儲在名為users.txt的文本文件中。 該程序采用以下格式:

user1,pass1
user2,pass2
user3,pass3
user4,pass4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM