1. Given the scores of students for Mathematics, English and Science, calculate the average score of each student and store it in a list.
2. Write the names, scores of each subject and average scores into a text file or csv file.
Code:
names = ['Zendaya', 'Yaacob', 'Xander', 'Willy',
'Vega', 'Umar', 'Takata', 'Sheena', 'Rhina', 'Queen']
math = [67, 73, 71, 11, 51, 46, 29, 51, 66, 25]
english = [87, 48, 93, 41, 69, 43, 0, 17, 78, 74]
science = [73, 71, 17, 81, 8, 33, 39, 33, 17, 93]
import csv
names = ['Zendaya', 'Yaacob', 'Xander', 'Willy',
'Vega', 'Umar', 'Takata', 'Sheena', 'Rhina', 'Queen']
math = [67, 73, 71, 11, 51, 46, 29, 51, 66, 25]
english = [87, 48, 93, 41, 69, 43, 0, 17, 78, 74]
science = [73, 71, 17, 81, 8, 33, 39, 33, 17, 93]
avg = [sum(grades) / len(grades) for grades in zip(math, english, science)]
fields = ['Name', 'Math', 'English', 'Science', 'Average']
filename = 'grades_records.csv'
with open(filename, 'w') as fout:
writer = csv.writer(fout)
writer.writerow(fields)
writer.writerows(zip(names, math, english, science, avg))
Comments
Leave a comment