Editor

Average Star Rating

#531 · Python Basics · Beginner · Find and fix · 30 XP · about 4 min

An online shop shows the average of its customer ratings, which are whole numbers from 1 to 5. average_rating(ratings) should return the average of the list rounded to one decimal place, and 0.0 for a new product that has no ratings yet.

The code in the editor has two bugs. Ratings of 4 and 5 show an average of 4 instead of 4.5, and opening a product with no ratings crashes the page with a ZeroDivisionError.

Examples

average_rating([4, 5])     # returns 4.5
average_rating([3, 4, 4])  # returns 3.7
average_rating([])         # returns 0.0

3 + 4 + 4 is 11, and 11 / 3 is 3.666..., which rounds to 3.7.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.