Catching a rejection raised by a helper function
Write code · Medium · §13.4, §13.5 The math helper gets sturdy
average is a helper function that returns the average of a list of view counts. If the list is empty or contains a negative number, it raises a ValueError. For each name in data, print the average like mina 100.0. If average rejects the list, print the message it raised with the error, like jun rejected: no records. Do not change average, and do not catch errors that are not rejections from average.
Starter code
def average(views):
if len(views) == 0:
raise ValueError("no records")
for v in views:
if v < 0:
raise ValueError("negative view count")
return sum(views) / len(views)
data = {"mina": [120, 80], "jun": [], "sora": [60, -5], "taeo": [30, 50, 40]}Solve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.