循环进行连续输入,从文件列表中输出数据,如果输入等于q则退出或退出 [英] Loop for continuous input, output data from list in file, quit if input is equal to q or quit

查看:185
本文介绍了循环进行连续输入,从文件列表中输出数据,如果输入等于q则退出或退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可根据用户输入从给定的文本文件中提取数据:

I have the following code that pulls data from a given text file based on the user input:

def read_file():

    students = []
    f = open('file.txt', 'r')
    for line in f.readlines():
        data = {}
        lines = ' '.join(line.split()).split()
        data[''] = int(lines[5])
        data[''] = float(lines[3])
        data[''] = float(lines[2])
        data[''] = line[1:6].rstrip(' ')
        students.append(data)

def search_student(students):
    student_search = input('Search for a student: ').lower()
    while student_search != 'q' and student_search != 'quit':
        for student in students:
            if student_search in student['Student Info'].lower():
                print_student_info(student)
                break
        student_search = input('Search for a student: ').lower()

该程序返回以下输出:

>>> Search for a student: 
>>> allen
>>> word: Allen, Age: 12, Height: 5'4, GPA: 3.8
>>> Search for a student:
>>> quit
>>> 

输入将提示程序根据用户输入的单词从文本文件中提取特定数据.这一切都很好.我无法弄清楚如何添加一个功能,如果在文件中找不到该单词,则会提示您再次输入.

The input will prompt the program to pull specific data from the text file based on the word that the user inputs. This all works fine. I'm not able to figure out how to add a feature that will prompt for another input if the word is not found in the file.

现在,如果在文件中找不到该单词,该程序将要求再次搜索.但是,在要求其他搜索之前,我需要说一些类似在文本文件中找不到单词"之类的东西.

Right now, if the word is not found in the file, the program just asks for another search. However, I need it to say something like "word not found in text file" before asking for another search.

这是我正在寻找的输出:

This is the output I'm looking for:

>>> Search for a student:
>>> Jim
>>> Word: Jim, Age: 20, Height: 6'1, GPA: 2.5
>>> Search for a student:
>>> Molly
>>> Your searched student could not be found.
>>> Search for a student:
>>> Eddy
>>> Word: Eddy, Age: 15, Height: 5'6, GPA: 4.0
>>> Search for a student:
>>> quit
>>> 

我觉得这样的事情应该起作用:

I feel like something like this should work:

def search_student(file):
student_search = input('Search for a student: ').lower()
while student_search != 'q' and student_search != 'quit':
    for student in students:
        if student_search in student['Student Info'].lower():
            print_student_info(student)
            student_search = input('Search for a student: ').lower()
        else:
            print("Your searched student could not be found.")
            student_search = input('Search for a student: ').lower()

但是它只是输出无论我输入什么词都找不到您所搜索的学生.即使键入使用第一个代码工作的学生,现在也不再起作用.即使键入quit也不再退出循环.请帮忙.

But it just outputs Your searched student could not be found no matter what word I input. Even typing a student that worked in the first code, now no longer works. Even typing quit no longer exits the loop. Please help.

新输出:

>>> Search for a student: 
>>> Allen
>>> Your searched student could not be found.
>>> Search for a student:
>>> quit
>>> Your searched student could not be found. 
>>> Search for a student:

推荐答案

所以我花了一些时间弄清楚问题出在哪里. 首先,我假设您的file.txt具有以下格式:

So I took some time to figure out what's the problem. First, I assumed that your file.txt has the following format:

Allan 12 5'4 3.8    
Eddy 15 5'6 4.0  

这些字段是姓名",年龄",身高",平均绩点(GPA)".因此,这里是读取文本文件的功能(请注意更改):

And the fields are Name, Age, Height, GPA. So here is the function to read the text file (please note changes):

def read_file():
    students = []
    f = open('file.txt', 'r')
    for line in f.readlines():
        data = {}
        lines = ' '.join(line.split()).split()
        data['Name'] = str(lines[0])
        data['Age'] = int(lines[1])
        data['Height'] = str(lines[2])
        data['GPA'] = float(lines[3])
        data[''] = line[1:4].rstrip(' ')
        students.append(data)
    return students

然后,我使用以下代码搜索学生信息:

Then for searching student information I used the following code:

def search_student(students):
    found = False
    student_search = input('Search for a student: ').lower()
    while student_search != 'q' and student_search != 'quit':
        for student in students:
            if student_search in student['Name'].lower():
                print_student_info(student)
                found = True
                break
        if found==False:
            print('Your searched student could not be found.')
        student_search = input('Search for a student: ').lower()

最后要打印学生信息:

def print_student_info(student):
    print('Word: {}, Age: {}, Height: {}, GPA: {}'.format(student['Name'],student['Age'],student['Height'],student['GPA']))    

在定义了这些功能之后,现在我们可以使用以下两行来运行代码:

After defining those functions, now we can run the code with these two lines:

students = read_file()
search_student(students)

这是我得到的结果:

Search for a student: molly
Your searched student could not be found.
Search for a student: david
Your searched student could not be found.
Search for a student: allan
Word: Allan, Age: 12, Height: 5'4, GPA: 3.8
Search for a student: q

这篇关于循环进行连续输入,从文件列表中输出数据,如果输入等于q则退出或退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆