How to make an auto grader with Python3

I am new to coding. I try my best to have a student grader whose name each student matches each average and shows the overall GPA among the students. This is my code:

Ken = [5,8,9]
Hiro = [10,11,20]
Nick = [20,20,20]

list1 = [Ken,Hiro,Nick]

total_average = 0
for j in list1:
   x = 0
   count = 0
   for i in j:
       x += i
       count += 1

   average = x / count
   print (round(average,2))
   total_average += average

final_average = total_average / len(list1)
print (round(final_average,2))

      

and the result is:

7.33
13.67
20.0
13.67

      

However, I really want to make a code that returns:

13.67, {'Ken': 13.67, 'Hiro': 20.0, 'Nick': 13.67}

      

How can I put it this way?

It would be very helpful if you could explain the details!

+3


source to share


3 answers


I would suggest another approach. First, you store your names and grades in a dictionary:

grades = {'Ken': [5,8,9], 'Hiro': [10,11,20], 'Nick': [20,20,20]}

      

Then you iterate over the dictionary and calculate the average for each student and prepare to calculate the global average:

averages = {}
sum_of_grades, number_of_grades = 0, 0
for name, marks in grades.items():
    averages[name] = sum(marks) / len(marks) # simple arithmetic average
    sum_of_grades += sum(marks)
    number_of_grades += len(marks)

      



Finally, you compute the global average:

final_average = sum_of_grades / number_of_grades

      

Here is the complete code where the computation is encapsulated in a function:

grades = {'Ken': [5,8,9], 'Hiro': [10,11,20], 'Nick': [20,20,20]}

def compute_averages(grades):
    averages = {}
    sum_of_grades, number_of_grades = 0, 0
    for name, marks in grades.items():
        averages[name] = sum(marks) / len(marks)
        sum_of_grades += sum(marks)
        number_of_grades += len(marks)
    final_average = sum_of_grades / number_of_grades

    return averages, final_average

averages, final_average = compute_averages(grades)
for name, avg in averages.items():
    print(name, round(avg, 2))
print('Final average', round(final_average, 2))

      

+1


source


Try using a dictionary instead of lists. Initialize dictionary: students = {'Hiro': 0, 'Ken': 0, 'Nick': 0}

Then assign values โ€‹โ€‹based on the calculated mean: students = {'Hiro': 20.0, 'Ken': 13.67, 'Nick': 13.67}



printing (students)

+1


source


I propose to introduce the student as a dictionary with {name, scores, average_score}

. All your students will be a list of dictionaries:

students = [
    {'name': 'Ken', 'scores': [5, 8, 9], 'average_score': 0.0},
    {'name': 'Hiro', ...},
    ...
]

      

You will now iterate over them like this:

for student in students:
    # Access the attributes like this:
    print(student['name'], student['scores'])

    # calculate your average value ...

    student['average_value'] = x/count
print(students) # you can print the whole dictionary and look at the results

      

With all the students on the list, it's easy to do the formatting you want - just skip all the students and type their name and their GPA in any order.

+1


source