python - 在 Python 中验证文本文件中的用户名和密码

标签 python

虽然我已经潜伏多年,但还是第一次发帖

我目前正在开发一个编程项目,但遇到了困难。该项目的目标是获取用户的输入、用户名和密码,并通过文本文件进行验证以查看其是否正确。如果正确,它将打印“欢迎!”并结束,如果没有,它将要求输入用户名和密码并循环,直到输入正确的组合。

文本文件采用以下格式:

Darth:Vader

Boba:Fett

R2:D2

ObiWan:Kenobi

Luke:Skywalker

这是我的代码:

# Lets user know what this program will do
print("Please login");

# Opens accounts txt file
accounts_file = open("accounts.txt", "r")

# Flag to terminate while loop
complete = False
# Runs loop until username and password are correct
while complete == False:
    # Asks user for username and stores as variable
    usernameInput = input("Username:");
    # Asks user for password and stores as variable
    passwordInput = input("Password:");
    # Reads each line of the text file line by line
    # Darth:Vader
    # Luke:Skywalker
    # R2:D2
    # ObiWan:Kenobi
    # Boba:Fett
    for line in accounts_file:
        # Stores each line in file as a username/password combo
        username, password = line.replace("\n","").split(":")
        # If username and password match, breaks out of the loop and sets complete to  True
        if usernameInput == username and passwordInput == password:
            complete = True
            break
        # Sets complete to False and loops back    
        else:
            complete = False
            continue
if complete == True:
    print("Welcome!");

如果第一次输入正确的名称,我可以让它正确运行: 例如:

Please login:

Username:Darth

Password:Vader

Welcome!

  • ends program

如果我输入错误的名称,它会像这样循环:

Please login:

Username:Darth

Password:Fett

Username:.....

但是,如果我输入错误的用户名然后正确的用户名,它不会像我想要的那样结束程序,并继续循环:示例:

Please login:

Username:Luke Password:Vader

Username:Darth Password:Vader

Username:R2 Password:D2

知道是什么导致它不接受用户的下一个输入并完成循环吗?

感谢您的帮助!

最佳答案

这样做是因为您的文件在您第二次尝试读取时为空。

第一次运行整个文件后:

for line in accounts_file:
        # Stores each line in file as a username/password combo
        username, password = line.replace("\n","").split(":")
        # If username and password match, breaks out of the loop and sets complete to  True
        if usernameInput == username and passwordInput == password:
            complete = True
            break
        # Sets complete to False and loops back    
        else:
            complete = False
            continue

文件为空,即所谓的“已耗尽”。 如果用户错了,您每次都需要重新创建一个新的文件对象。就这样做:

accounts_file = open("accounts.txt", "r")
for line in accounts_file:
        # Stores each line in file as a username/password combo
        username, password = line.replace("\n","").split(":")
        # If username and password match, breaks out of the loop and sets complete to  True
        if usernameInput == username and passwordInput == password:
            complete = True
            break
        # Sets complete to False and loops back    
        else:
            complete = False
            continue
account_file.close() # Very important to close the file for next time!

或者你可以使用 with ... as ... 像这样:

with open("accounts.txt", "r") as accounts_file:
    for line in accounts_file:
        # Stores each line in file as a username/password combo
        username, password = line.replace("\n","").split(":")
        # If username and password match, breaks out of the loop and sets complete to  True
        if usernameInput == username and passwordInput == password:
            complete = True
            break
        # Sets complete to False and loops back    
        else:
            complete = False
            continue

关于python - 在 Python 中验证文本文件中的用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68962951/

相关文章:

python - 将许多 txt/csv 文件编译成单个数据框,并将文件名添加为列

python - 如何进行递归子文件夹搜索并返回列表中的文件?

python - Django:在测试客户端上设置 cookie?

python - 生成/写入 XML 文件的标准方式

python - 如何使 time.mktime 与 datetime.fromtimestamp 一致工作?

Python 错误 : TypeError: a bytes-like object is required, 不是 'str'

python - 使用 Python 仅复制 XLS 的工作表以成为新 XLS 中的新工作表?

python - 如何找到 Pandas 中多列的比率?

python - 比较两组之间的每个元素?

python - 将 pd datetime 对象更改为整数