Start learning free

LESSON 202 OF 287 · PROJECTS: EVERYDAY PROGRAMS

Report Card V6 in Python: max, min and Average of Scores

Practice Read the full explanation

Compute three class statistics from a list of scores: the highest, the lowest, and the average.

You start with:

scores = [92, 78, 85, 67, 74, 88, 91, 58]

Build:

Highest: 92
Lowest: 58
Average: 79

Rules:

  • highest is the largest value in scores.
  • lowest is the smallest value in scores.
  • average is the sum divided by the count, using integer division (//).
  • Store the three results in variables named highest, lowest and average. The checker prints the three lines for you, so do not print anything yourself.

Use max(scores), min(scores), and sum(scores) // len(scores).

scores = [92, 78, 85, 67, 74, 88, 91, 58]

highest = max(scores)
lowest = min(scores)
average = sum(scores) // len(scores)

STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Report Card V6 in Python, explained

    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

    1. Create highest with max(scores). One call looks at the whole list.
    2. Create lowest with min(scores), the mirror of max.
    3. Create average as sum(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), not max(scores[0]). They also accept several separate arguments, as in max(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, lowest and average exactly, 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([]) raises ValueError: max() iterable argument is empty, and sum([]) // len([]) raises ZeroDivisionError, because sum([]) is 0 and len([]) 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 be 79.125, not the whole number the tests want.
    • Naming a variable max or sum. An assignment such as max = 92 hides the built-in function, and a later call to max(scores) would fail because an integer isn't callable. That is why the lesson names the results highest and lowest.
    • Dividing by a typed number. Writing // 8 works until the list changes length. len(scores) follows the list.

    Key takeaways

    • max, min, sum and len each 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.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free