Average Rating, Skipping Gaps

Python for AIMedium+10 XP+10

Average Rating, Skipping Gaps

A film festival collects audience ratings in a CSV file. Not everyone rated every film, so some cells are empty.

Task: write average_of(path, text, column_name). It returns the average of the numbers in the column called column_name, as a float.

  • An empty value ("") means "no rating". Leave it out completely, so it doesn't count as 0 and isn't part of the count.
  • Every other value in that column is a number, possibly with a decimal point.
  • If there are no ratings to average, return 0.0.
  • The column always exists, but it can be anywhere in the row.

The starter code includes a small helper, write_text, and already calls it for you: it saves text into a file named path. Your code then opens that file and reads it. import csv is already at the top of the starter code.

Examples

path = 'fest.csv'
text = 'viewer,film_a,film_b\nann,4,\nbo,2,5\ncy,,3\n'
column_name = 'film_a'
returns 3.0
path = 'fest2.csv'
text = 'viewer,film_a,film_b\nann,4,\nbo,2,5\ncy,,3\n'
column_name = 'film_b'
returns 4.0

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