在 python 中检查文件中的单词后如何生成单个输出

how to produce a single output after checking a file for a word in python

我是新 python 用户,最近开始在 python 中创建一个测验项目。我想为每个参加测验的人做一个帐户。我将所有用户名和密码保存在一个文本文件中,如果用户想要登录,程序将检查文件中的用户名和密码。这是我的代码,但是当我 运行 它打印出来文件中文本每一行的输出。我只想要一个基于整个文件的输出。有人知道如何解决这个问题吗?:

 choice = input("are you registered user?")
    if choice == "1":
        age = input ("age? ")
        name = input ("name? ")
        yrgroup = input("yr group? ")
        username = name[:3] + age 
        print ("your username is ", username)
        password = input ("password? ")
        students = open("students.txt","a")
        students.write(password)
        students.write(" ")
        students.write(age)
        students.write(" ")
        students.write(yrgroup)
        students.write(" ")
        students.write(username)
        students.write(" ")
        students.write(name)
        students.write(" ")
        students.write("\n")
        students.close()
    elif choice == "2":
        user = input ("please enter your username: ")
        pas = input ("please enter password: ")
        with open("students.txt","r") as file:
            for line in file:
                word = line.split(" ")
                if pas in word:
                    print ("LOGGING IN")
                else:
                    print ("WRONG")
    else:
        print("invalid input")

当我 运行 它打印这个:

 hello everyone
    are you registered user?2
    please enter your username: ale15
    please enter password: meow
    WRONG
    WRONG
    WRONG
    LOGGING IN

我需要它只输出一行,说明是错误还是登录。

你可以做到

user = input ("please enter your username: ")
    pas = input ("please enter password: ")
    result = False
    with open("students.txt","r") as file:
        for line in file:
            word = line.split(" ")
            if pas in word:
                result = True
    if (result):
        print('LOGGIN IN')
    else:
        print('WRONG')