I have a random set of test scores for 50 students. Each student answered 20 questions, and the maximum score for each question is 5. How can I get the total score of each student in a list by using a Python function?
results_sheet = []
num_of_questions = 20
num_students = 50
#generate scores for each student and append to results_sheet
for student in range(num_students):
scores = random.choices(list(range(0,6)),k=num_of_questions)
results_sheet.append(scores)
#The result here is a list of lists.
So if I call the results for 2 students, I will get the following:
print(results_sheet[0:2])
[[4, 2, 3, 4, 2, 0, 0, 1, 2, 4, 0, 3, 5, 4, 3, 2, 5, 0, 3, 3], [5, 1, 1, 3, 2, 0, 2, 5, 3, 1, 1, 3, 5, 1, 4, 3, 2, 4, 5, 4]]
I have tried the following code but am missing something. Or perhaps the code is simply wrong:
def sum_score(results_sheet):
total = 0
for val in results_sheet:
total = total + val
return total
The end result should look something like this for 2 students:
total_scores = [47, 55]
result_sheet[num_students][num_of_questions][*total_scores] = map(sum, results_sheet).