This is my programming task:
The file "grades.txt" contains data about the tests taken by students and the achieved grade. Each line in the file contains the name of the student, the test the student took, and the achieved grade. You can assume that no two students share the same name. Furthermore, a student cannot retake the test if a passing grade was achieved.
Write a function print_finished(filename) that takes as argument a filename and prints the names of all students that have finished school. A student has finished school if all tests have been taken and in each test a grade of at least 2 has been achieved.
For example, in the "grades.txt." file, student "Magnus'" grades are:
Magnus, A, 7
Magnus, D, 0
Magnus, D, 0
Magnus, D, 12
Magnus, B, -2
Magnus, C, 12
Magnus, B, -2
Magnus, B, 4
Magnus, E, 4
Since he retook test B and D and passed both, "Magnus" has finished school. Another "student" in the "grades.txt" file "Noah" got a -2 on their retaken D-test and thus, has not finished school.
--- MY CODE AND QUESTION ---
I need a function to convert a file into a dicitonary/list (not sure which one) that prints out the name of the "student" if all the final tests (including retaken) has a grade higher or equal to 2.
I have made my attempt with this code:
def print_finished(filename):
students = {}
for line in open(filename):
entry = line.rstrip().split(", ")
name = entry[0]
grade = int(entry[2])
if grade >= 2:
students[name] = grade
for student,grade in sorted(students.items()):
print(student, grade)
gradelist = r"grades.txt"
print_finished(gradelist)
Unfortunately, this my attempted function only takes the first instance of a student passing a test into account. So if a student passed their first test, they are in the list, but if a student failed their first test but passed it in a retaken exam, they are not included.
What do I need to do so the function prints the names of all students that passed their final tests with a grade larger than or equal to 2?