3.14 Libraries Homework: Season Stats Report

Build a program that reports on a player’s season using at least three standard library modules.

Your version must

  • Use at least 3 standard library modules (this one uses json, datetime, and statistics)
  • Have a procedure like summarize_season that returns the total points, average points, best game, and points against each opponent
  • Document the API: a docstring giving the parameters and the return value
  • Use datetime to report how many days are left until the next game
  • Write a requirements.txt listing pandas and Flask, with a comment explaining why json and datetime are not listed
  • Use your own sport, your own games, and your own next game date

Submission safety rules

  • Keep the # CODE_RUNNER: comment as the first line of the cell
  • The code must run with no errors
  • Only import standard library modules (or flask); pandas is not available in the code runner
  • Do not use input()

Description box (paste when submitting)

Code Runner Challenge

Homework Hack - Season Stats Report

View IPYNB Source
Lesson: CSP 3.14 Libraries
Popcorn 1: what I changed = <your change>
Popcorn 2: my extra route = <your route address>
Homework: libraries used = json, statistics, datetime
Homework: days until my next game = <value>
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Grading (1 point)

  • 0.2 Popcorn completion (0.1 each)
  • 0.2 libraries, 0.2 procedure, 0.2 API documentation, 0.1 datetime, 0.1 requirements.txt
# CODE_RUNNER: Homework Hack - Season Stats Report

import json
import statistics
from datetime import date

# Swap in your own sport, games, and next game date
games = [
    {"date": "2026-09-14", "opponent": "Poway", "points": 21, "minutes": 27},
    {"date": "2026-09-18", "opponent": "Westview", "points": 29, "minutes": 31},
    {"date": "2026-09-22", "opponent": "Poway", "points": 17, "minutes": 24},
    {"date": "2026-09-26", "opponent": "Mt. Carmel", "points": 33, "minutes": 32},
    {"date": "2026-09-29", "opponent": "Westview", "points": 25, "minutes": 28},
]

next_game = date(2026, 10, 15)

def summarize_season(season):
    """
    Summarize a list of games.

    Parameters:
        season: list of dictionaries, each with "date", "opponent", "points", and "minutes"
    Returns:
        dictionary with total_points, average_points, best_game,
        and points_by_opponent
    """
    points = [game["points"] for game in season]

    # max() with key= finds the whole game dictionary with the most points
    best = max(season, key=lambda game: game["points"])

    # Add up the points scored against each opponent
    points_by_opponent = {}
    for game in season:
        opponent = game["opponent"]
        points_by_opponent[opponent] = points_by_opponent.get(opponent, 0) + game["points"]

    return {
        "total_points": sum(points),
        # statistics.mean is a tested library procedure, so no hand-written loop is needed
        "average_points": round(statistics.mean(points), 1),
        "best_game": {"date": best["date"], "opponent": best["opponent"], "points": best["points"]},
        "points_by_opponent": points_by_opponent,
    }

report = summarize_season(games)

# datetime subtracts two dates and gives a timedelta; .days is the number of days
days_left = (next_game - date.today()).days
report["days_until_next_game"] = days_left

# json.dumps turns the dictionary into readable JSON text
print(json.dumps(report, indent=2))
print("Days until my next game:", days_left)

# requirements.txt for a real version of this app (pandas for the season table,
# Flask for the scoreboard page)
requirements_txt = """# json, statistics, and datetime are NOT listed because they are part of
# Python's standard library, so they already come with Python.
# Only external libraries need pip install.
pandas
Flask
"""

print("\nrequirements.txt:")
print(requirements_txt)