Session M1-02 lab: Diagnose and extend a calculator with Chat¶
Duration: 2 hours
Difficulty: Beginner
Prerequisites: Session M1-01 and Python 3.10 or later
Deliverable: A corrected and extended calculator, a passing test suite, and a
completed chat-notes.md
Lab overview¶
You will use different Chat surfaces on one Python calculator project. A focused test exposes a weighted-average bug. After fixing it, you will add missing tests, answer a workspace question, and handle a change request that requires a judgment about zero total weight.
| Part | Work | Time |
|---|---|---|
| 1 | Prepare the project and reproduce the failure | 15 min |
| 2 | Explain and diagnose the defect | 25 min |
| 3 | Apply the fix and expand tests | 30 min |
| 4 | Review the project with workspace and terminal context | 20 min |
| 5 | Decide and implement the change request | 20 min |
| 6 | Compare, document, and check | 10 min |
Before you start¶
Use synthetic data in a training repository you may use for the exercise.
Before starting, check GitHub Copilot Chat access in your editor. If Chat does not work, stop the lab. Manual implementation does not replace the Chat and inline-suggestion practice.
If model comparison is allowed, use only choices approved for the exercise and set any required metered-work stop condition. If no second choice is available, compare the assisted result with the reference solution.
Part 1: Prepare and reproduce (15 minutes)¶
cd sessions/session-m1-02-chat-and-inline/lab/starter
python -m py_compile calculator.py test_calculator.py
python -m unittest -v
Three tests should pass. The weighted-average test should fail because the result
is 190 instead of 95.
Read calculator.py, test_calculator.py, change-request.md, and
chat-notes.md.
Checkpoint: You can state the failing input, actual result, and expected result without asking Chat to guess them.
Part 2: Explain and diagnose (25 minutes)¶
Use inline Chat or the sidebar with the weighted_average selection.
Start with an explanation request:
Explain the weighted_average method line by line. State the mathematical formula
it implements. Do not propose a fix yet.
Compare the response with the failing test. Then ask:
The test expects weighted_average([80, 100], [1, 3]) to equal 95, but the method
returns 190. Identify the defect. Limit the answer to this method.
If the surface supports an explain shortcut, try it with the same selection.
Record the useful context and the diagnosis in chat-notes.md.
Checkpoint: The diagnosis identifies sum(weights) as the denominator.
Part 3: Fix and expand tests (30 minutes)¶
Request a narrow fix:
Fix only weighted_average so it divides by the total weight. Preserve the public
method signature and the existing length check. Add no dependency.
Review the proposed diff before accepting it. Run:
Next, use a tests shortcut or a direct prompt to propose tests for:
- mismatched list lengths;
- empty lists;
- history recording;
- a zero total weight.
Do not accept tests that invent behavior. Leave the zero-total-weight rule for the change request in Part 5.
Checkpoint: The original four tests pass. Proposed new tests are separated from the accepted contract.
Part 4: Use workspace and terminal context (20 minutes)¶
Ask a workspace-level question:
@workspace Summarize the Calculator public behavior and identify which methods
have focused tests. Cite the relevant files.
If the current surface uses a different workspace-context mechanism, use its documented equivalent.
Run the tests again. Then use terminal context, if available:
Explain the most recent test result. Which behavior failed or passed, and which
file owns the next change?
Check the response yourself. Terminal context can summarize output; it does not replace reading the failure.
Checkpoint: Your notes distinguish code behavior, test output, and Chat interpretation.
Part 5: Decide the change request (20 minutes)¶
Open change-request.md.
The request says to reject a zero total weight but leaves the exception type and message to you. Choose both. The reference solution uses:
Add a focused test first. Then update weighted_average. While reviewing the
method, also decide how empty inputs should behave. Record both decisions in
chat-notes.md.
Run:
Checkpoint: The suite covers the original defect, mismatch, empty input, zero total weight, and history.
Part 6: Compare and check (10 minutes)¶
If you may use two model choices, send both the same prompt:
Review weighted_average and its tests. List any accepted behavior that lacks a
test. Do not change code.
Compare only accuracy you can check and useful citations. Otherwise compare the
Chat-assisted result with lab/solution/.
Inspect the final diff:
git --no-pager diff -- calculator.py test_calculator.py chat-notes.md
python -m py_compile calculator.py test_calculator.py
python -m unittest -v
Final deliverable¶
calculator.pyuses total weight and rejects invalid weighted-average inputs.test_calculator.pycovers the accepted behavior.chat-notes.mdrecords the chosen surfaces, prompts, check results, judgment call, and comparison path.
Verification¶
- Both Python files compile.
- All eight reference tests pass.
- The code, test, and notes state the zero-total-weight decision.
- Chat changes stayed inside the calculator project.
- No new dependency was added.