Problem overview
Version 6 of the Report Card project summarises a whole class. Given a list scores = [92, 78, 85, 67, 74, 88, 91, 58], compute three variables: highest, the largest score; lowest, the smallest; and average, the sum divided by the count with integer division.
The expected output reads Highest: 92, Lowest: 58 and Average: 79. The lesson's tests print those lines, so your code only has to create the three variables.
Approach: three built-in functions
Earlier versions found values with loops. For these three statistics, Python already has built-ins that take a whole list: max(scores) returns the largest item, min(scores) the smallest, and sum(scores) the total. The count comes from len(scores). No loop is needed.
For the starter list, the sum is 633 and there are 8 scores. The exact average is 79.125. Integer division with // drops the fraction and gives 79, as the lesson expects, while / would give the float 79.125.
Step by step
- Create
highestwithmax(scores). One call looks at the whole list. - Create
lowestwithmin(scores), the mirror ofmax. - Create
averageassum(scores) // len(scores): the total divided by the number of scores, rounded down.
The solution in Python
scores = [92, 78, 85, 67, 74, 88, 91, 58]
highest = max(scores)
lowest = min(scores)
average = sum(scores) // len(scores)
The details that matter:
- The functions take the list itself. Write
max(scores), notmax(scores[0]). They also accept several separate arguments, as inmax(3, 9, 4), but here the data is already in a list. - The average uses floor division. It rounds toward negative infinity, which for non-negative scores is the same as dropping the fraction. For
[100, 99]the exact average is 99.5, and//gives 99. - The names must match. The variables are called
highest,lowestandaverageexactly, because the lesson's tests read them by those names.
What the built-ins do for you
Each function replaces a small loop. Finding the highest score by hand means starting with highest = scores[0], visiting every score and replacing highest whenever a score is bigger. Summing means starting a total at 0 and adding each score. For the starter list, that hand-written version gives a highest of 92, a total of 633 over 8 scores, and an average of 79, the same answers as the built-ins. The built-ins are shorter and leave less room for a mistake, so Python code uses them whenever they fit.
Testing
[70]gives a highest of 70, a lowest of 70 and an average of 70.[100, 99]gives 100, 99 and 99, because 199 // 2 is 99.
Edge cases and common mistakes
- A one-item list. With
[70], the highest, the lowest and the average are all 70. - An empty list.
max([])raisesValueError: max() iterable argument is empty, andsum([]) // len([])raisesZeroDivisionError, becausesum([])is 0 andlen([])is 0. The lesson's list always has scores, but a program that accepts any list would check for emptiness first. - Using
/. The average would be79.125, not the whole number the tests want. - Naming a variable
maxorsum. An assignment such asmax = 92hides the built-in function, and a later call tomax(scores)would fail because an integer isn't callable. That is why the lesson names the resultshighestandlowest. - Dividing by a typed number. Writing
// 8works until the list changes length.len(scores)follows the list.
Key takeaways
max,min,sumandleneach answer one question about a list in one call.- The average is
sum(...) // len(...)when a whole number is wanted. - Empty lists need a check of their own.
