Introduction
Hitters get hot. Analysts track recent hits per game to spot who is trending up.
Today you will build a mini trend tracker that scans recent games and finds hot streaks.
What we're building
- Store recent game stats in a list
- Loop through the list to find big games
- Track streaks to see how long the hot stretch lasts
- Load a CSV game log and analyze real data
Part 1: Quick Start
Download the Starter Pack
>button: Download the Starter PackOpen the starter pack in Cursor by going to File > Open Folder and selecting the folder you downloaded from your computer.
You should see a file called trend_tracker.py. Open it, this is where we will be writing our code.
Download the Baseball Game Log
>button: Download Baseball Game Log CSVMove the downloaded game_log.csv into the same folder as trend_tracker.py.
Writing the code
Type the code below into trend_tracker.py:
player_name = "Your Player"
recent_stats = [2, 1, 4, 1, 3]
print(f"Trend Tracker for {player_name}")
print("Recent games:", recent_stats)
Running the code
Save the file (Cmd + S or Ctrl + S)
Run the code by clicking the "play" icon in the top right of the screen. Or open the terminal (Cmd + ~ or Ctrl + ~) and type python trend_tracker.py.
If everything is working correctly, you should see:
Trend Tracker for Your Player
Recent games: [2, 1, 4, 1, 3]
Great! You just created your first list of game stats.
Here's what you need to know about lists:
- List stores multiple items in a single variable (like a shopping list!)
- Lists can hold numbers, text, or other data types
- Items in a list stay in order and you can change them
- Each item has a position number (called an index), starting at 0 for the first item
Part 2: Project Milestones
Milestone 1: Print every game in the list
A list is great, but we want to look at each game individually. That is where loops come in.
for stat in recent_stats:
print(stat)
Expected output:
2
1
4
1
3
Here's what you need to know about loops:
- A loop lets you go through each item in a list one by one
- The
forloop repeats the same code for every item - Think of it like checking each game on your list — you look at game 1, then game 2, then game 3...
Milestone 2: Flag hot games above a threshold
Now we will define what counts as a "hot" game. For hits, let us use 3 hits.
The if statement lets your program make decisions. Think of it like this: "If the stat is high enough, then print it!" The program checks each game — if it meets your threshold, it prints it. If not, it moves on to the next game.
hot_threshold = 3
for stat in recent_stats:
if stat >= hot_threshold:
print(f"Hot game: {stat}")
Expected output:
Hot game: 4
Hot game: 3
Milestone 3: Track the longest hot streak
Let us track how many hot games in a row the player had. This is a streak counter.
hot_threshold = 3
current_streak = 0
best_streak = 0
for stat in recent_stats:
if stat >= hot_threshold:
current_streak = current_streak + 1
if current_streak > best_streak:
best_streak = current_streak
else:
current_streak = 0
print(f"Best hot streak: {best_streak}")
Expected output:
Best hot streak: 1
Here's what's happening with if and else:
- The
ifstatement checks: "Is this stat hot enough?" If yes, add 1 to the streak. - The
elsestatement handles what happens when the answer is no: reset the streak to 0. - Think of it like this: "If the game is hot, keep counting. Otherwise, start over!"
Try changing the list to include multiple hot games in a row and run it again.
Milestone 4: Load stats from a CSV file
Lists are useful, but real data is often stored in files. Use the game_log.csv you downloaded earlier.
A CSV (Comma Separated Values) file is a plain text file where each line represents a data record, and fields within each record are separated by commas.
The file looks like this:
date,stat
2025-01-03,2
2025-01-05,1
2025-01-08,4
Now load it in Python:
import csv
stats_from_file = []
with open("game_log.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
stats_from_file.append(int(row["stat"]))
print("Loaded stats:", stats_from_file)
Once you can load the stats, replace recent_stats with stats_from_file and re-run your trend tracker.
If you made it this far, you have built a real streak tracker. Nice work.
Bonus Exercises: Push It Further with Your Agent
Use your AI assistant to explore more ideas with the same building blocks (lists, loops, and files).
Bonus 1: Add Dates to the Output
Goal: Print both the date and the stat for every game.
Try prompts like:
- "Show me how to store both date and stat in a list and print them together."
- "Update my CSV loader so it prints
date - statfor each row."
Bonus 2: Calculate an Average
Goal: Find the average stat value across the games.
Try prompts like:
- "Help me calculate the average of a list of numbers."
- "Print the average and compare it to my hot threshold."
Bonus 3: Visualize the Trend with ASCII Bars
Goal: Turn each stat into a simple bar chart in the terminal.
Try prompts like:
- "Print a row of
#characters for each stat value." - "Make the bars scale so big games look longer."
Bonus 4: Compare Two Players
Goal: Load two lists of stats and compare who had the longer hot streak.
Try prompts like:
- "Help me load two CSV files and compare their best streaks."
- "Print which player is heating up more."
Bonus 5: Trend Alerts
Goal: Print a special message when the player is on a hot streak of 3 or more.
Try prompts like:
- "If the best streak is 3 or more, print a trend alert message."