How to calculate percentage and average of test scores in a 2D list without using libraries like pandas or numpy

Question:

I have a csv data of a test scores. The current program is able to read this data into a 2D list with the test out of marks. I later created a function to remove test out of row so only the student’s marks can be displayed. I’m now struggling to write a function which can print the scores so that each student’s percentage appears on a separate line of output.

My code so far

def getData():

   with open("testscores.csv","r") as file:
    lineArray = file.read().splitlines()
    matrix = []
    for line in lineArray:
      matrix.append(line.split(","))

    return matrix

def fullScores(matrix):
  matrix.pop(0)

  return matrix

def printscores(matrix):

  for counter in matrix:
    for values in counter:
      print(values, end= " ")
    print()
  

matrix = getData()
matrix = fullScores(matrix)
print()
printscores(matrix)

output

Bob 10 9 7 8 10 9 9 9 10 8 8 10 9 9 
Sue 8 8 8 9 4 8 9 7 8 3 10 10 7 9
Jan 6 6 0 5 7 9 4 7 8 5 7 1 5 9
Sam 8 8 8 7 7 7 9 9 9 9 8 9 10 8
Tom 9 9 9 9 9 9 9 9 9 10 9 9 9 9

expected output

Bob 100% 90% 70% 80% 100% 90% 90% 90% 100% 80% 80% 100% 90% 90% Average = 89%
Sue 80% 80% 80% 90% 40% 80% 90% 70% 80% 30% 100% 100% 70% 90% Average = 77%
...

csv data

Testoutof,10,11,12,11,10,11,9,10,10,11,10,12,10,9
Bob,10,9,7,8,10,9,9,9,10,8,8,10,9,9
Sue,8,8,8,9,4,8,9,7,8,3,10,10,7,9
Jan,6,6,0,5,7,9,4,7,8,5,7,1,5,9
Sam,8,8,8,7,7,7,9,9,9,9,8,9,10,8
Tom,9,9,9,9,9,9,9,9,9,10,9,9,9,9
Asked By: Mateo

||

Answers:

Of course, there are many ways to do this, but here is one possible solution. I used only Lists and Tuple. Using dictionaries you would have a more elegant way accessing data.

students = []
reference_scores = []

def get_data():
    with open("./data/testscores.csv", "r") as file:
        lineArray = file.read().splitlines()        

        for line in lineArray:
            raw_data = line.split(",")
            name = raw_data[0]
            scores = []
            percentages = []
            average_percentage = 0

            if name != 'Testoutof':
                scores = raw_data[1:]

                for index, score in enumerate(scores):
                    percentage = get_percentage(int(score), int(reference_scores[index]))
                    average_percentage += percentage
                    percentages.append(percentage)
                average_percentage //= len(scores)
                students.append( (name, percentages, average_percentage) )
            else:
                reference_scores = raw_data[1:]
    return students

def get_percentage(score, reference):
    return (score * 100) // reference

def print_scores(data):
    for obj in data:
        score_string = ''
        for score in obj[1]:
            score_string += f'{score}% '    
        print(f'{obj[0]} {score_string} Average = {obj[2]}')

students = get_data()
print_scores(students)

# Output
Bob 100% 81% 58% 72% 100% 81% 100% 90% 100% 72% 80% 83% 90% 100%  Average = 86
Sue 80% 72% 66% 81% 40% 72% 100% 70% 80% 27% 100% 83% 70% 100%  Average = 74
Jan 60% 54% 0% 45% 70% 81% 44% 70% 80% 45% 70% 8% 50% 100%  Average = 55    
Sam 80% 72% 66% 63% 70% 63% 100% 90% 90% 81% 80% 75% 100% 88%  Average = 79 
Tom 90% 81% 75% 81% 90% 81% 100% 90% 90% 90% 90% 75% 90% 100%  Average = 87
Answered By: Niko

Using list comprehension can give you the same result, without any function definition:

file=open("testscores.csv","r")
matrix=[line.strip().split(",") for line in file.readlines()[1:]]

for row in matrix:
  data=[int(i)*10 for i in row[1:]]
  print(row[0], "% ".join(map(str, data))+"%",  "Average =", sum(data)//len(data),"%")

Output:

Bob 100% 90% 70% 80% 100% 90% 90% 90% 100% 80% 80% 100% 90% 90% Average = 89 %
Sue 80% 80% 80% 90% 40% 80% 90% 70% 80% 30% 100% 100% 70% 90% Average = 77 %
Jan 60% 60% 0% 50% 70% 90% 40% 70% 80% 50% 70% 10% 50% 90% Average = 56 %
Sam 80% 80% 80% 70% 70% 70% 90% 90% 90% 90% 80% 90% 100% 80% Average = 82 %
Tom 90% 90% 90% 90% 90% 90% 90% 90% 90% 100% 90% 90% 90% 90% Average = 90 %
Answered By: AziMez
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.