代码只会读取文本文件的第一行

时间:2018-10-05 10:56:22

标签: python

所以我从这里的另一篇文章中复制了一些代码,但是我无法在第一篇文章之后读取任何行,我试图创建用户名和密码系统。

def register():                                     #This function has been adapted from www.stackoverflow.com
    print("Please register.")
    time.sleep(1)
    username = input("Please input your desired username: ")
    password = input("Please input your desired password: ")
    file = open("outputusernamefile.txt","a")
    file.write(username)
    file.write(" ")
    file.write(password)
    file.write("\n")
    file.close()
    print("Please login now.")
    time.sleep(1)
    logged = True
    login()


def login():                                        #This function has been adapted from www.stackoverflow.com
    print("Please enter your credentials.")
    time.sleep(1)
    login.username = str(input("Please enter your username: "))
    password = str(input("Please enter your password: "))
    for line in open("outputusernamefile.txt","r+").readlines(): # Read the lines
        login_info = line.split() # Split on the space, and store the results in a list of two strings
        if login.username == login_info[0] and password == login_info[1]:
            print("Correct credentials!")
            print("You are now logged in.")
            logged = True
            QuizStart()
            return True
        else:
            print("Incorrect credentials.")
            print("Please try again.")
            time.sleep(0.5)
            login()
            return False    

3 个答案:

答案 0 :(得分:2)

您将在第一次迭代中退出循环。这就是为什么只检查一行。也许尝试做这样的事情。

def login():
    ...
    with open("outputusernamefile.txt","r+") as file:
        for line in file.readlines():
            login_info = line.split() # Split on the space, and store the results in a list of two strings
            if login.username == login_info[0] and password == login_info[1]:
                print("Correct credentials!")
                print("You are now logged in.")
                logged = True
                QuizStart()
                return True

    print("Incorrect credentials.")
    print("Please try again.")
    time.sleep(0.5)
    login()

答案 1 :(得分:1)

login函数中的递归调用从头开始读取文件,因为再次打开了文件。

您需要阅读整个文件,寻找匹配项,然后在循环末尾做出最终决定。如果该函数找到了所请求的用户名,则只能提早返回。

答案 2 :(得分:0)

这是它对我有用的方式。您忘了逐行迭代。

import time

def register():                                     #This function has been adapted from www.stackoverflow.com
    print("Please register.")
    time.sleep(1)
    username = input("Please input your desired username: ")
    file = open("outputusernamefile.txt","a")
    file.write(username)
    file.write(" ")
    password = input("Please input your desired password: ")
    file.write(password)
    file.write("\n")
    file.close()
    print("Please login now.")
    time.sleep(1)
    logged = True
    login()


def login():                                        #This function has been adapted from www.stackoverflow.com
    print("Please enter your credentials.")
    time.sleep(1)
    login.username = str(input("Please enter your username: "))
    password = str(input("Please enter your password: "))
    with open("outputusernamefile.txt","r+") as file: # Read the lines
        line = file.readline()
        while line:
            login_info = line.split() # Split on the space, and store the results in a list of two strings
            if login.username == login_info[0] and password == login_info[1]:
                user_found = True
                print("QuizStart()")
                return True
            else:
                time.sleep(0.5)
                user_found = False
            line = file.readline()

    if not user_found:
        print("Incorrect credentials.")
        print("Please try again.")
        login()
    else:
        print("Correct credentials!")
        print("You are now logged in.")


register()