"""
Lesson 25 — Exercise 4: The Debugging Toolkit
================================================
Goal: Hunt the SAME bug with three different tools.

You will:
  1. Find the bug using PRINT STATEMENTS
  2. Find it again using VSCODE BREAKPOINTS + STEPPING
  3. Add a WATCH on the variable that exposes it
"""


# The bug is real. Read carefully — but don't stare too long, you'll
# spot it. The point of this exercise is to feel each TOOL, not to be clever.

def calculate_average_grade(grades):
    """Return the average of grades, EXCLUDING grades below 50.

    Spec: a grade of exactly 50 SHOULD be included.
    Spec: an empty list (no qualifying grades) should return 0.0.
    """
    total = 0
    count = 0
    for grade in grades:
        if grade > 50:        # ← something here is wrong
            total += grade
            count += 1
    return total / count       # ← something here is also fragile


sample_grades = [45, 50, 60, 75, 80, 95]
# Expected (per spec):  mean of [50, 60, 75, 80, 95] = 72.0
# What does the buggy version actually return? Find out below.


# ═══════════════════════════════════════════════════════════════
# TASK A — DEBUG WITH PRINT STATEMENTS
# ═══════════════════════════════════════════════════════════════
# Copy the function below and add print() calls to show:
#   - which grades pass the filter
#   - the running total and count after each iteration
#   - the final values just before dividing
# Then run it and trace the output.

def calculate_average_grade_with_prints(grades):
    # TODO
    total = 0
    count = 0
    for grade in grades:
        if grade > 50:
            total += grade
            count += 1
            # TODO: print here
    # TODO: print here
    return total / count


# ═══════════════════════════════════════════════════════════════
# TASK B — DEBUG WITH BREAKPOINTS (VSCode)
# ═══════════════════════════════════════════════════════════════
# 1. Open this file in VSCode.
# 2. Click the gutter on the line "if grade > 50:" to set a breakpoint (red dot).
# 3. Press F5 to start debugging (pick Python File).
# 4. When execution pauses, use F10 (Step Over) to advance one line at a time.
# 5. Watch the VARIABLES panel on the left — `grade`, `total`, `count` update live.
#
# QUESTION: at which value of `grade` does the bug become visible?
# Write your answer here:
# ANSWER:


# ═══════════════════════════════════════════════════════════════
# TASK C — ADD A WATCH
# ═══════════════════════════════════════════════════════════════
# In VSCode's debug sidebar, find the WATCH panel.
# Add an expression to watch: `total / max(count, 1)`
# Step through again. The watch updates after every line of execution.
#
# Now FIX the bugs:
#   - Change `>` to `>=` so grade == 50 is included
#   - Guard the division so an empty filtered list doesn't crash
# Run again with sample_grades and the empty list [] and confirm
# both produce the expected values.


# ═══════════════════════════════════════════════════════════════
# REFLECTION (write your answers in comments)
# ═══════════════════════════════════════════════════════════════
# 1. Which tool was FASTEST to set up?
# 2. Which tool gave you the MOST information once set up?
# 3. In what kind of situation would prints still be the better choice
#    even though breakpoints are more powerful?


if __name__ == '__main__':
    print("Buggy version:", calculate_average_grade(sample_grades))
    print("Your prints version:")
    calculate_average_grade_with_prints(sample_grades)
