-1

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

This program returns the following output:

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

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.

New output:

>>> 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:
5
  • Can you please add your input? Commented May 8, 2020 at 20:35
  • I added it. Can you help? Commented May 8, 2020 at 20:40
  • I can but please give me your file Commented May 8, 2020 at 20:45
  • @Rose16 you are iterating over file but not using that Commented May 8, 2020 at 20:48
  • This is just one section of my code. Please help me figure out how to fix just this portion of code. Commented May 8, 2020 at 20:52

4 Answers 4

0

There is one mistake in the code. it says for f in file but then it uses file again. I think you need to change that part to this:

        for f in file:
            if user_search in f['Data Info'].lower():
                print(data_info)
                break
Sign up to request clarification or add additional context in comments.

1 Comment

I made this change to the code, and still i didn't work.
0

Problem in your code is you are using loop to search for the word. If first word in file is not equal to your searched word it will enter else condition. You need to move this else condition to execute after end of the for loop.

def info_search(file):
    user_search = input('Search for a word: ').lower()
    while user_search != 'q' and user_search != 'quit':
        for f in file:
            if user_search in f['Data Info'].lower():
                print(data_info)
                user_search = input('Search for a word: ').lower()
        else:
            print("Your searched word could not be found.")
            user_search = input('Search for a word: ').lower()
    return

1 Comment

komatiraju032 This worked for only one loop. after it goes through the loop once, it only says "Your searched word could not be found" for every input.
0

I don't know what is your data and what is Data Info but if you have just file you may use the code below.

def info_search(lines):
    user_search = ''
    while True:
        user_search = input('Search for a word: ').lower()
        if user_search in ("q", "quit"):
            break
        for line in lines:
            if user_search in line.lower():
                print("Your searched word[{0}] found.".format(user_search))
                break
        else:
            print("Your searched word[{0}] could not be found.".format(user_search))

with open(filename, 'r') as file:
    lines = file.readlines()

info_search(lines)

Comments

0

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  

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

And finally for printing student info:

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)

This is the results I got:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.