Editor

Average on Hot Days

Intermediate · Write the code · from Extracting Information from Data: Cleaning, Filtering and Metadata

From the lesson: How do programs filter, transform and combine data?

A handful of processes do most of the work of extracting or changing information: - Transforming changes every element of a data set in the same way, such as converting every temperature to Celsius or adding a price to every sales record.

Read this part of the lesson

Write average_on_hot_days(days, threshold). days is a list of dictionaries with the keys "cups" and "temp_f". Return the average number of cups sold on days whose temp_f is at least threshold, rounded to 1 decimal place. If no day is hot enough, return None.

days = [{'cups': 52, 'temp_f': 85}, {'cups': 18, 'temp_f': 63}, {'cups': 61, 'temp_f': 91}]
average_on_hot_days(days, 80)  ->  56.5
def average_on_hot_days(days, threshold):
# Filter the days, then combine their cups into an average
pass

What Check runs

Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

RunsExpected output
print(average_on_hot_days([{"cups": 52, "temp_f": 85}, {"cups": 18, "temp_f": 63}, {"cups": 61, "temp_f": 91}], 80))56.5
print(average_on_hot_days([{"cups": 30, "temp_f": 72}, {"cups": 40, "temp_f": 78}], 70))35.0
print(average_on_hot_days([{"cups": 30, "temp_f": 72}], 90))None

More Data Filtering exercises

All Python exercises · 50 practice problems