Add a Total Column

Python for AIHard+10 XP+10

Add a Total Column

A teacher has a gradebook CSV. The first column is the student's name, and every other column is one test's mark. The school wants a copy of the file with an extra total column at the end.

Task: write add_total(in_path, in_text, out_path). Read the gradebook from in_path and write a new CSV to out_path with:

  • the same header row, plus "total" at the end,
  • every student row, plus that student's total at the end: the sum of every mark, leaving out the name.

Then read out_path back with csv.reader and return the list of lists.

  • Every mark is a whole number. Totals are written as whole numbers too, so 14, not 14.0.
  • A student's name can contain a comma. The reader and writer handle that for you.
  • If there are no students, the output file is just the header row with "total" added.

The starter code includes write_text and already uses it to save in_text into in_path. import csv is already at the top. Open the output file with newline="".

Examples

in_path = 'marks.csv'
in_text = 'name,quiz,exam\nada,4,10\nbo,7,3\n'
out_path = 'marks_out.csv'
returns [['name', 'quiz', 'exam', 'total'], ['ada', '4', '10', '14'], ['bo', '7', '3', '10']]
in_path = 'marks2.csv'
in_text = 'name,t1\n"Lovelace, Ada",9\n'
out_path = 'marks2_out.csv'
returns [['name', 't1', 'total'], ['Lovelace, Ada', '9', '9']]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run