> Source URL: /ai-and-sports/projects/4-team-breakdown/team-breakdown.baseball.project
---
title: Team Breakdown - Become a Sports Analyst (Baseball)
student_outcomes:
  - use comparison operators to evaluate game results
  - use conditionals to label wins and losses
  - filter game data using multiple conditions
  - summarize team performance from a CSV file
---

# Project: Team Breakdown - Become a Sports Analyst (Baseball)

---

## Introduction

Analysts do not just look at final scores. They break down **how** a team wins and loses across a season: scoring, pace, efficiency, and patterns that repeat.

In this project, you will build a small program that scans a season-length game log and answers questions like:

- Which games were wins?
- Which wins were also high scoring?
- Which opponents caused trouble?
- What patterns show up across the season?

### What we're building

By the end of this project, you will:

- **Use conditionals** to label wins and losses
- **Filter games** based on score, result, and pace
- **Search and group** games to find patterns
- **Sort** games to find the biggest wins and losses
- **Load a CSV file** to analyze season-style data

### Why this matters

Filtering data is one of the most important skills in analytics. It lets you answer specific questions quickly and gives you evidence to back up an argument.

---

## Part 1: Quick Start

### Download the Starter Pack

[>button: Download the Starter Pack](./team-breakdown-starter-pack.zip)

Open 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 `team_breakdown.py`. Open it, this is where we will be writing our code.

### Download the Baseball Season CSV

[>button: Download Baseball Season CSV](./data/baseball/team_games.csv)

Move the downloaded `team_games.csv` into the same folder as `team_breakdown.py`.

### Writing the code

Type the code below into `team_breakdown.py`:

```python
team_name = "Your Team"
team_score = 6
opponent_score = 3

print(f"{team_name} scored {team_score} and allowed {opponent_score}")

if team_score > opponent_score:
    print("Result: Win")
else:
    print("Result: Loss")
```

### Running the code

Save the file (<Kbd>Cmd + S</Kbd> or <Kbd>Ctrl + S</Kbd>)

Run the code by clicking the "play" icon in the top right of the screen. Or open the terminal (<Kbd>Cmd + ~</Kbd> or <Kbd>Ctrl + ~</Kbd>) and type `python team_breakdown.py`.

If everything is working correctly, you should see:

```
Your Team scored 6 and allowed 3
Result: Win
```

Nice! You just used a conditional to decide a win or loss.

---

## Part 2: Project Milestones

### Milestone 1: Store multiple games in a list

Now we will store multiple games so we can analyze a full stretch of the season. We will include the opponent and whether the game was home or away.

```python
games = [
    ["Apr 01", "Bears", "Home", 6, 3],
    ["Apr 03", "Wolves", "Away", 2, 5],
    ["Apr 05", "Hawks", "Home", 8, 7],
    ["Apr 07", "Knights", "Away", 1, 4]
]
```

Each inner list represents `[date, opponent, home_away, team_score, opponent_score]`.

---

### Milestone 2: Label each game as a win or loss

Loop through the list and use a conditional to print the result for each game.

```python
for game in games:
    date = game[0]
    opponent = game[1]
    home_away = game[2]
    team_score = game[3]
    opponent_score = game[4]

    if team_score > opponent_score:
        result = "Win"
    else:
        result = "Loss"

    print(f"{date} vs {opponent} ({home_away}): {team_score}-{opponent_score} ({result})")
```

Expected output:

```
Apr 01 vs Bears (Home): 6-3 (Win)
Apr 03 vs Wolves (Away): 2-5 (Loss)
Apr 05 vs Hawks (Home): 8-7 (Win)
Apr 07 vs Knights (Away): 1-4 (Loss)
```

---

### Milestone 3: Filter high-scoring, high-pace wins

Pace can be approximated by **total runs in the game**. Let us filter only the games that were wins and also high pace.

```python
high_score = 6
high_pace = 9

for game in games:
    team_score = game[3]
    opponent_score = game[4]
    pace = team_score + opponent_score
    efficiency = team_score / opponent_score

    if team_score > opponent_score and team_score >= high_score and pace >= high_pace and efficiency >= 1.1:
        print(f"High-pace win: {team_score}-{opponent_score} (pace {pace}, eff {efficiency:.2f})")
```

Expected output:

```
High-pace win: 6-3 (pace 9, eff 2.00)
High-pace win: 8-7 (pace 15, eff 1.14)
```

---

### Milestone 4: Load games from a CSV file

Use the `team_games.csv` you downloaded earlier to analyze a longer stretch of games.

The file looks like this:

```
date,opponent,home_away,team_score,opponent_score
2025-04-01,Bears,Home,6,3
2025-04-03,Wolves,Away,2,5
2025-04-05,Hawks,Home,8,7
```

Use this code to load it:

```python
import csv

games_from_file = []

with open("team_games.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        games_from_file.append([
            row["date"],
            row["opponent"],
            row["home_away"],
            int(row["team_score"]),
            int(row["opponent_score"])
        ])

print("Loaded games:", games_from_file)
```

Once the data is loaded, replace `games` with `games_from_file` and re-run your filters.

---

### Milestone 5: Search, group, and sort

Now let us dig for deeper patterns.

**Search by opponent**

```python
target_opponent = "Bears"

for game in games:
    opponent = game[1]
    team_score = game[3]
    opponent_score = game[4]

    if opponent == target_opponent:
        print(f"Game vs {opponent}: {team_score}-{opponent_score}")
```

**Group by home vs away (count wins)**

```python
home_wins = 0
away_wins = 0

for game in games:
    home_away = game[2]
    team_score = game[3]
    opponent_score = game[4]

    if team_score > opponent_score:
        if home_away == "Home":
            home_wins += 1
        else:
            away_wins += 1

print(f"Home wins: {home_wins}, Away wins: {away_wins}")
```

**Sort by biggest margin**

```python
def margin(game):
    return game[3] - game[4]

sorted_games = sorted(games, key=margin, reverse=True)

print("Top 3 margins:")
for game in sorted_games[:3]:
    print(f"{game[0]} vs {game[1]}: {game[3]}-{game[4]} (margin {margin(game)})")
```

---

If you made it this far, you have built a simple analysis pipeline that can answer real questions from game data.

## Bonus Exercises: Push It Further with Your Agent

Use your AI assistant to explore more ideas with conditionals and filtering.

### Bonus 1: Count Wins and Losses

Goal: Print the total number of wins and losses.

Try prompts like:

- "Help me count wins and losses in my list of games."
- "Print a record like 10-4."

### Bonus 2: Find Close Games

Goal: Filter games decided by 1 run.

Try prompts like:

- "Show me how to check if the score difference is 1."
- "Print all close games with the run difference."

### Bonus 3: Home vs Away Split

Goal: Add a home/away column and compare results.

Try prompts like:

- "Update the CSV loader to include a home_away column."
- "Calculate the win percentage for home games vs away games."

### Bonus 4: Add a Streak Counter

Goal: Track the longest winning streak.

Try prompts like:

- "Use a streak counter to find the longest win streak."
- "Print a message if the streak is 3 or more."

### Bonus 5: Generate a Simple Report

Goal: Print a short summary paragraph with key findings.

Try prompts like:

- "Write a summary sentence with my total wins, losses, and high-scoring wins."



---

## Backlinks

The following sources link to this document:

- [>button: ⚾ Baseball](/ai-and-sports/projects/4-team-breakdown/team-breakdown.project.llm.md)
