because you want to overwrite the same file (is it a good idea?) you need to read the data from input file, calculate mean, store data in some data structure in memory and after you process all data - write to a file with same name.
import csv from statistics import mean with open ('grades.csv', 'r') as input_file: reader=csv.reader(input_file) data = [] for row in reader: name = row[0] grades=[int(num) for num in row[1:]] # alternatively use: grades = map(int, row[1:]) row.append(mean(grades)) # alternatively use: row.append(f'{mean(grades):.2f}') data.append(row) with open('grades.csv', 'w', newline='') as output_file_name: writer = csv.writer(output_file_name) writer.writerows(data)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs