Scenario: You are a data analyst for a weather station. Daily maximum temperatures for a week are stored in two separate lists: one for the days of the week and another for the temperatures in Celsius.
The arrays are parallel: the first element in days matches the first element in temperatures_celsius, and so on.
Data
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
temperatures_celsius = [12, 14, 8, 5, 15, 17, 16]
Your tasks
Solve this by iterating through the arrays (do not use sum(), max(), min() or .index()).
- Find the hottest and coldest days — keep track of the index of the max and min temperatures so you can print the day name with each temperature.
- Calculate the average temperature for the week and print it.
- Count pleasant days — a pleasant day is between 10°C and 16°C inclusive. Print the count.
💡 Try the whole challenge in your editor first. Only reveal the solution when you have had a proper go.
Solution
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
temperatures_celsius = [12, 14, 8, 5, 15, 17, 16]
hottest_index = 0
coldest_index = 0
total = 0
pleasant = 0
for i in range(len(temperatures_celsius)):
temp = temperatures_celsius[i]
total = total + temp
if temp > temperatures_celsius[hottest_index]:
hottest_index = i
if temp < temperatures_celsius[coldest_index]:
coldest_index = i
if temp >= 10 and temp <= 16:
pleasant = pleasant + 1
average = total / len(temperatures_celsius)
print("Hottest day of the week:", days[hottest_index], "with", temperatures_celsius[hottest_index], "°C")
print("Coldest day of the week:", days[coldest_index], "with", temperatures_celsius[coldest_index], "°C")
print("Average temperature for the week:", round(average, 2), "°C")
print("Number of pleasant days:", pleasant)
Example output:
Hottest day of the week: Saturday with 17 °C
Coldest day of the week: Thursday with 5 °C
Average temperature for the week: 12.43 °C
Number of pleasant days: 4