首页 > 解决方案 > 登录程序不起作用,无论输入什么

问题描述

登录程序,使用名为“UserAccounts.txt”的文件

def login():
    print("logging in, ok")
    counter = 0
    while counter <= 5:
         Username = input("Username:")
         Password = input("Password:")
         users = open("UserAccounts.txt","r")
         entry = False
     ########### v checks file v #############
         for record in users:
             if record == Username:
                 Pass = record
                 if Pass == Password:
                     print("logged in")
                     entry = True
         if entry == False:
             print("Incorret, try again")
             counter = counter + 1


     print("LOCKED: Tried over 5 times")

无论输入如何,代码始终输出:

logging in, ok
Username:El
Password:Password
Incorret, try again
Username:

你知道为什么吗?(当用户和密码在文件中时)

感谢您的帮助,祝您有美好的一天!(可能很简单的问题,但大脑不工作)

标签: pythonpython-3.x

解决方案


逐行阅读时,行是用逗号分隔的,所以在声明为Usernameor之前需要用逗号分隔Pass

def login():
    print("logging in, ok")
    counter = 0
    while counter <= 5:
        Username = input("Username:")
        Password = input("Password:")
        users = open("UserAccounts.txt","r")
        entry = False
    ########### v checks file v #############
        for record in users:
            if record.split(',')[0] == Username:
                Pass = record.split(',')[1] #assuming 34 is pwd. [2] if 'Secure' is the pwd
                if Pass == Password:
                    print("logged in")
                    entry = True #maybe replace this line with "return" to stop while loop
        if entry == False:
            print("Incorret, try again")
            counter = counter + 1

推荐阅读