python无法使用loggin密码脚本

python无法使用loggin密码脚本,python,Python,我正在学习构建一个python脚本来检查文本文件中的用户名和密码,以及是否正确记录用户。将有两个用户,文件将有多个用户。 我有脚本工作,但我有一些密码检查问题。我想让函数在密码输入错误的情况下进行循环。 如果我在函数中执行此操作而陷入同一个循环中,我只能通过退出函数并再次调用它来让它工作 下面是有效的脚本。如果密码不正确,则返回0,0,因此调用函数 user1Status = 0 user2Status = 0 def logging(): filex = open('users.tx

我正在学习构建一个python脚本来检查文本文件中的用户名和密码,以及是否正确记录用户。将有两个用户,文件将有多个用户。 我有脚本工作,但我有一些密码检查问题。我想让函数在密码输入错误的情况下进行循环。 如果我在函数中执行此操作而陷入同一个循环中,我只能通过退出函数并再次调用它来让它工作

下面是有效的脚本。如果密码不正确,则返回0,0,因此调用函数

user1Status = 0
user2Status = 0

def logging():
    filex = open('users.txt', 'r')
    x = filex.read()  # This will store the contents of the file as a string
    filex.close()
    login_info = x.split()
    numberUsers = (len(login_info))
    username = input("enter username")
    count = 0
    for x in range(0, numberUsers):
        if username != login_info[x]:
            count = count + 1
    if numberUsers == count:
        print('Username not recognised')
        return 0, 0
    password = input("enter password")
    for x in range(0, numberUsers):
        if username == login_info[x] and password == login_info[x + 1]:
            status = 1
            return username, status
        else:
            print('Your password is incorrect')
            #logging()
            return 0, 0

while 1:
    if user1Status == 0:
        print ('User1 please logging')
        username1, user1Status = logging()
    if user1Status == 1 and user2Status == 0:
        print('User2 please logging')
        username2, user2Status = logging()
    if (user1Status == 1) and (user2Status == 1):
        print("Welcome back %s and %s" % (username1, username2))#
如果我在logging函数的末尾注释掉return 0,0和uncomment logging(),那么如果输入了错误的密码,那么它将继续围绕该函数循环,然后返回正确的密码。这是一件不能走这条路的事情


我尝试这样做的原因是。如果密码输入不正确,我不想再次询问用户是否有帐户

我使用以下脚本成功地使它工作。我添加了一个if语句来检查密码。这是完整的代码

user1Status = 0
user2Status = 0

def logging():
    status = 0
    attempt = 0
    filex = open('users.txt', 'r')
    x = filex.read()  # This will store the contents of the file as a string
    filex.close()
    login_info = x.split()
    numberUsers = (len(login_info))
    username = input("enter username")
    count = 0
    for x in range(0, numberUsers):
        if username != login_info[x]:
            count = count + 1
    if numberUsers == count:
        print('Username not recognised')
        return 0, 0
    while (attempt!=3):
        password = input("enter password")
        for x in range(0, numberUsers):
            if username == login_info[x] and password == login_info[x + 1]:
                status = 1
        if status == 1:
            return username, status
        else:
            print("Password incorrect")
            attempt = attempt+1
        if (attempt == 3):
            print('you have used max attempts to enter password')
            return 0, 0

def register():
    username = input('Enter a user name: ')
    password = input('Enter a password: ')
    fw = open('users.txt', 'a')
    fw.write('\n')
    fw.write(username)
    fw.write(' ')
    fw.write(password)
    fw.close()
    return(username, 0)

def start():
    q1 = input("Do you have an account?")
    if q1.lower() == "yes":
        x, y = logging()
        return (x, y)
    if q1.lower() == "no":
        x, y = register()
        return (x, y)
    else:
        print ("please enter yes or no")
        return (0,0)

while 1:
    if user1Status == 0:
        print ('User1 please logging')
        username1, user1Status = start()
    if user1Status == 1 and user2Status == 0:
        print('User2 please logging')
        username2, user2Status = start()
    if (user1Status == 1) and (user2Status == 1):
        print("Welcome back %s and %s" % (username1, username2))
        print("")
        print("1: role dice")
        print("2: user1 Logout")
        print("3: user2 Logout")
        q2 = input("Select from one of the options above: ")
        if q2 == '2':
            print("User 1 has logged out")
            user1Status = 0
        if q2 == '3':
            print("User2 has logged out")
            user2Status = 0

你能举例说明一下
users.txt
是什么样子的吗?它是一个带有
用户名的文本文件全部在一行上?看看您构建代码的方式,似乎就是这样。您可能还没有遇到它,但我可以马上指出的一个问题是,一旦两个用户都登录,您的程序将反复打印“欢迎回来”消息,您需要将其删除。文本文件是用户名密码每个用户位于单独的行上,用户名和密码之间只有一个空格。我理解你所说的欢迎回来的意思。我有更多的代码,我只是为了这篇文章把它删掉了