0

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]
3
  • Are you resetting the results_sheet between each loop iteration? Commented Jul 21, 2021 at 8:32
  • What that piece of code you provided returns. Because i'm not sure how the result_sheet looks like. Is it 2D list looking like result_sheet[num_students][num_of_questions] Commented Jul 21, 2021 at 8:34
  • Pythonically [*total_scores] = map(sum, results_sheet). Commented Jul 21, 2021 at 8:49

4 Answers 4

2

You can easily use numpy.sum, and set axis to negative.

import random
import numpy

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)

If you want the sum of all:

numpy.sum(results_sheet, axis=-1)

array([43, 44, 61, 64, 52, 44, 48, 51, 36, 50, 51, 45, 56, 47, 53, 60, 53,
       45, 45, 46, 53, 54, 39, 42, 54, 67, 44, 48, 51, 58, 53, 54, 40, 52,
       62, 54, 61, 58, 47, 38, 51, 45, 50, 49, 61, 30, 50, 59, 54, 34])

So if I call the results for 2 students:

numpy.sum(results_sheet, axis=-1)[0:2]
array([43, 44])
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome. you can also use the following setting, numpy.sum(results_sheet[A:B], axis=0)[X:Y] to know the result of students between A and B on the X:Y questions.
1

Try a list comprehension:

final = [sum(lst) for lst in results_sheet]

Or use map:

final = list(map(sum, results_sheet))

Edit:

To use a function:

def func(results_sheet):
    return [sum(lst) for lst in results_sheet]
print(func(results_sheet))

3 Comments

Thank you, @U11-Forward. Both solutions were helpful. What if I wanted to use a function to solve the problem?
@CTan Check my Edit: part, please accept and upvote if it helps
@CTan I did the function in the recent edit
1

First, if you are having a return statement outside of a function it will not work. Secondly, you can also just use the build-in sum function in Python:

sum_of_all = sum(scores)

Then you can do this for each individual student's scores:

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)

sums = [sum(result_sheet) for result_sheet in results_sheet]

The sums variable will be a list of summed scores for the students, in the same order as the generated scores.

Comments

1

You have to apply sum() on every 2nd level list, like below

import random

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)


total = 0
for val in results_sheet:
    total = total + sum(val)
print(total)

2 Comments

Actually you don't need the second for loop, you can do the sum in the first loop total = total + sum(scores), the code will be faster. Of course you have to set total = 0 before the 1st loop.
Thank you, that helps in getting the total score for all students.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.