Editor

Average Mark From a File

#560 · File handling · Beginner · 35 XP · about 7 min

A teacher saves the class marks in a text file, one student per line: the student's name, a space, then their mark. Some names have several words, so the mark is always the last word on the line. The file may also contain blank lines, which you skip.

Write average_mark(filename) that returns the average of all the marks, rounded to one decimal place with round(total / count, 1). If the file holds no marks at all, return 0.0.

Example

If marks.txt holds:

Asha 80
Ben 75
Anna Marie Lee 90

then average_mark('marks.txt') returns 81.7, because 245 / 3 = 81.666...

Note

Every mark is a whole number from 0 to 100.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.