繁体   English   中英

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

[英]Code will only read the first line of a text file

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

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    

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

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()

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

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

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

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM