Python 3.14 - Libraries HW
Python 3.14 Libraries
Completed both Popcorn Hacks and the Season Stats Report Homework Hack (basketball), with API documentation and a requirements.txt example.
Popcorn Hack 1: Read the API, Then Use It
I changed the jersey range to 1–99 and used a 400-square-foot court, so math.sqrt(400) always gives 20.0. The random game and jersey change every run.
Code Runner Challenge
3.14 Popcorn 1 - use math and random APIs
View IPYNB Source
# CODE_RUNNER: 3.14 Popcorn 1 - use math and random APIs
import math
import random
# Reading the docs tells us what each function takes and returns.
print("random.randint:", random.randint.__doc__)
print("math.sqrt:", math.sqrt.__doc__)
points = [14, 22, 9, 17, 20]
print("Random game:", random.choice(points), "points")
print("Random jersey (1-99):", random.randint(1, 99))
print("Court side length:", math.sqrt(400), "feet")
average = sum(points) / len(points)
print("Average:", average, "-> rounded up:", math.ceil(average))
Popcorn Hack 2: Write a Flask Route
The roster is customized and a new /coach route uses the same pattern as /score and /roster.
Code Runner Challenge
3.14 Popcorn 2 - customize Flask routes with a browser-safe fallback
View IPYNB Source
# CODE_RUNNER: 3.14 Popcorn 2 - customize Flask routes with a browser-safe fallback
try:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/score")
def score():
return jsonify({"home": 58, "away": 51})
@app.route("/roster")
def roster():
return jsonify({"players": ["Leo", "Nia", "Omar"]})
@app.route("/coach")
def coach():
return jsonify({"coach": "Coach Kim", "focus": "free throws"})
client = app.test_client()
print("/score:", client.get("/score").get_json())
print("/roster:", client.get("/roster").get_json())
print("/coach:", client.get("/coach").get_json())
except ModuleNotFoundError:
# If Flask isn't installed, show the same route data so the cell still runs.
print("/score:", {"home": 58, "away": 51})
print("/roster:", {"players": ["Leo", "Nia", "Omar"]})
print("/coach:", {"coach": "Coach Kim", "focus": "free throws"})
Homework Hack: Basketball Season Stats Report
Uses four standard-library modules: json, statistics, datetime, and math. season_report() has a docstring that explains its parameters and what it returns.
Code Runner Challenge
3.14 Homework - basketball season stats report
View IPYNB Source
# CODE_RUNNER: 3.14 Homework - basketball season stats report
import json
import math
import statistics
from datetime import date
games = [
{"opponent": "Eagles", "points": 14, "minutes": 28},
{"opponent": "Hawks", "points": 22, "minutes": 32},
{"opponent": "Eagles", "points": 9, "minutes": 20},
{"opponent": "Lions", "points": 17, "minutes": 30},
]
def season_report(games):
"""Summarize a season.
Parameters: games - list of dictionaries with opponent, points, and minutes.
Returns: dictionary with total_points, average_points, best_game, and points_by_opponent.
"""
points = []
by_opponent = {}
for game in games:
points.append(game["points"])
name = game["opponent"]
if name in by_opponent:
by_opponent[name] = by_opponent[name] + game["points"]
else:
by_opponent[name] = game["points"]
return {
"total_points": sum(points),
"average_points": statistics.mean(points),
"best_game": max(points),
"points_by_opponent": by_opponent,
}
report = season_report(games)
# datetime: days until the next game (0 if it has passed)
report["days_until_next_game"] = max(0, (date(2026, 11, 1) - date.today()).days)
# math: total minutes rounded up to whole hours
total_minutes = 0
for game in games:
total_minutes = total_minutes + game["minutes"]
report["hours_played"] = math.ceil(total_minutes / 60)
# json: print the report as JSON text
print(json.dumps(report, indent=2))
requirements.txt
pandas
Flask
# json, math, statistics, and datetime come with Python, so they are not listed.
Only outside packages go in requirements.txt. pandas and Flask have to be installed with pip; the four modules used above are built into Python.