-1

Can someone help...

My driver file is here:

from functions import process_marks 

def main():
    try:
        f = open(argv[1])
    except FileNotFoundError:
        print("\nFile ", argv[1], "is not available")
        exit()
    process_marks(f)
    f.close()

main()

I'm trying to modify process_marks(f) to run my code. and produce this (from file that runs without functions):

Names of students who have written tests:
Anthony Austyn Bronson Conor Mark
Enter name of student whose test results you wish to see: Anthony

Summary of Test Results for Anthony
===================================
Test scores:  85 85 85 85
Number of tests written ..................  4

This is what I currently have:

names = []
name_print = "\nSummary of Test Results for "

def process_marks(file_name):
    names
    for line in file_name:
        names.append(line.split()[0])
    print('\nNames of students who have written tests:')
    print(*sorted(names), sep=' ')
    name = input('Enter name of student whose test results '
            'you wish to see: ')
    check_name(name)
    parts = line.split()
    if parts[0] == name:
        print_scores(name)



def check_name(person):
    if person not in names:
        print('\nNo test data found for ', person)
        input("Press Enter to continue ...")
    else:
        print(name_print + person)        


def print_scores(person, parts):
        print('=' * ((len(name_print)) - 1))
        test_scores = ' '.join(parts[1:])
        print('Test scores: ', end=' ')
        print(test_scores)

Which outputs:

Names of students who have written tests:
Anthony Austyn Bronson Conor Mark
Enter name of student whose test results you wish to see: Anthony

Summary of Test Results for Anthony

I need help making print_scores() function work in process_marks().

Can someone see where my errors lie?

Gizmo
  • 21
  • 1
  • 1
  • 8
  • What is `parts` in the `print_scores` function? It is never defined. Also why are you checking `if names[0] == name`? This means your script will only work for the first name... Also why your first line is just `names`? This has no effect – Tomerikoo Feb 29 '20 at 18:41
  • parts in print_scores is now defined in process_marks(). I want the program run like so: Loop through .txt and pull the first index from each line (name). Print out the names on one line. Prompt the user to type which name they want to see. Then based on that input, if the name is in names: print the scores which are the following indexes of whatever line the name is on – Gizmo Feb 29 '20 at 18:52

1 Answers1

1

Your error (mainly) lies in the fact that you are comparing the input name to the last row of the file. This is because you check if parts[0] == name where parts = line.split(). This means the parts are of the last row of the file always - no matter what name provided.


To fix this, I would start by a better way of storing your data. Right now you are just saving the names in a list. But what about the grades? I think a better solution would be to use a dict. So start by changing to:

names = {}  # instead of []

Now you want to fill that dict with the names as keys (keeps the logic similar as with the list) and the list of grades as the value for that key. So your file parsing can look like:

for line in file_name:
    elements = line.split()
    names[elements[0]] = elements[1:]
# names now look like: {'Anthony': ['85', '85', '85', '85'], 'Conor': [...], ... }

Now another thing is that you call the check_name function but then make another check in process_marks. This seems redundant. I would change the ceck_name function to return a boolean indicating if the name is ok or not:

def check_name(person):
    if person not in names:
        print('\nNo test data found for ', person)
        input("Press Enter to continue ...")
        return False
    else:
        print(name_print + person)
        return True

And in process_marks you can use it as:

name = input('Enter name of student whose test results you wish to see: ')
if check_name(name):
    print_scores(name)

Lastly, regarding the parts issue. Now you have the grades stored in names along with the matching name they belong to. So all we have left to do is change print_scores to take only one argument grades and use that instead of parts, and from process_marks just call it:

print_scores(names[name])

A view of a possible complete code:

names = {}
name_print = "\nSummary of Test Results for "

def process_marks(file_name):
    with open(file_name) as f:
        for line in f:
            elements = line.split()
            names[elements[0]] = elements[1:]
    print(names)
    print('\nNames of students who have written tests:')
    print(*sorted(names), sep=' ')
    name = input('Enter name of student whose test results you wish to see: ')
    if check_name(name):
        print_scores(names[name])

def check_name(person):
    if person not in names:
        print('\nNo test data found for ', person)
        input("Press Enter to continue ...")
        return False
    else:
        print(name_print + person)
        return True

def print_scores(grades):
    print('=' * (len(name_print) - 1))
    test_scores = ' '.join(grades)
    print('Test scores: ', end=' ')
    print(test_scores)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • This is good. However, i think its a bit too far out of my relm of what im comfortable with. Im trying to become good at modifying and working with lists with this particular problem and i dont want to go too far from my current code. I asked the question another way here: https://stackoverflow.com/questions/60469255/function-help-converting-a-program-that-takes-in-a-txt-file-and-modifying-to-functions – Gizmo Feb 29 '20 at 20:18
  • @Gizmo I really don't understand what you mean by *too far out of my relm*. The only thing different is the use of a dict which is to store the grades with each name. I mean, storing the names in a list is fine, but how would you get the grades for that name? You will have to loop the file each time to look for that name. If you will always stick to what you're comfortable with, you will never advance. You should be open to learn and embrace new stuff, especially basic ones like a dict. This is just a tip, not criticism. Lastly, don't open many questions regarding the same issue... – Tomerikoo Feb 29 '20 at 22:54