Skip to content Skip to sidebar Skip to footer

Converting A Program That Takes In A .txt File And Modifying To Functions

Completely stuck on functions.... I have a .txt file: Bronson 90 85 75 76 Conor 90 90 90 90 Austyn 50 55 32 75 Mark 96 95 85 85 Anthony 85 85 85 85 And I'm trying to take this c

Solution 1:

Your method-breakdown is a bit off.

For example this line:

print_grades(name, line)

Will use the last value of line from the for loop above. Which will always be "Anthony"

You can try to pass the values from method to method as below:

from sys import argv


defget_names(my_file):
    names = []
    for line in my_file:
        names.append(line.split()[0])
        print('\nNames of students who have written tests:')
        print(*sorted(names), sep=' ')
    return names


defget_and_check_name(names):
    name = input('Enter name of student whose test results ''you wish to see: ')
    if name notin names:
        print('\nNo test data found for ', name)
        input("Press Enter to continue ...")
    else:
        name_print = "\nSummary of Test Results for " + name
        return name, name_print


defprint_scores(f, name, name_print):
    for line in f:
        parts = line.split()
        if parts[0] == name:
            print(name_print)
            print('=' * ((len(name_print)) - 1))
            test_scores = ' '.join(parts[1:])
            print('Test scores: ', end=' ')
            print(test_scores)


defprocess_marks(f):
    While True:
        names = get_names(f)
        name, name_print = get_and_check_name(names)
        f.seek(0)
        print_scores(f, name, name_print)
        yes_no = input('\nDo again for another student?')
        if yes_no == 'n':
            breakcontinuewithopen(argv[1]) as f:
    process_marks(f)

Post a Comment for "Converting A Program That Takes In A .txt File And Modifying To Functions"