首页 > 解决方案 > 我的登录系统将无法工作 - 无论输入任何用户密码

问题描述

我的注册和登录身份验证系统无法正常工作。我的注册问题已解决,但我的登录有问题。代码要么让我通过并访问帐户,要么不会,这取决于代码。但无论何时我尝试修复它,输出都是两个选项之一。总是,不管我输入什么密码。

用户名和密码存储在一个 txt 文件中,如下所示:

John Appleseed:hisSuperSecretPassword
JohnDoe:1234

登录代码:

found = False
username = input("Enter your username:\n")
file = open("account.txt", "r+")
    
for line in file:
    if line.split(':')[0] == username:
        found = True
if found == True:
    password = input("Enter your password:\n")

    for counter, line in enumerate(file):
            
        if line.strip() == username + ":" + password:
            print("You have signed in.")
        else:
            print("Password incorrect. Program closing.")
            sys.exit()
else:
    print("Username not valid.")
    sys.exit()

任何人都可以帮忙吗?运行 Python 3.9.2。

标签: pythonauthentication

解决方案


这是我为工作而调整的东西....

import sys

found = False
username = input("Enter your username:\n")
file = open("account.txt", "r+")
    
for line in file:
    if line.split(':')[0] == username:
        account_details = line.split(':')
        found = True

if found == True:
    password = input("Enter your password:\n")
    if account_details[1].strip() == password:
        print("You have signed in.")
    else:
        print("Password incorrect. Program closing.")
        sys.exit()
else:
    print("Username not valid.")
    sys.exit()

推荐阅读