Week 12 Coding Lab: The Community Garden Harvest Log
Part 1 — The Big Picture
This week you learned that a file keeps text after a program ends, that write mode starts a file over and write adds no newline of its own, that read hands back one string and readlines a list of lines that still end in their newlines, that every line read back is text until int converts it, that a with statement closes the file for you, and that try and except catch a runtime error by its type while a bare except hides real bugs. In this lab you put those ideas to work for an illustrative community garden's harvest log. You predict what six short programs print, run them, and study every gap between your prediction and the run. Then you read three broken programs, fix each with the smallest change that works, write a small save-and-reload program of your own, and check a chatbot's answers to the same questions by running the code, because a chatbot predicts likely text and does not run your program.
Time budget: Plan for about 90 minutes.
Part 2 — The Task
Guiding question: For the garden's harvest log, which of these six predictions about files and exceptions will you get wrong, and how will each run show you what a file really holds, when a line is still text, and which except block runs?
Workshop data: Community garden harvest log: six short programs and three broken programs
Source: Six short Python programs and three broken programs about an illustrative community garden's harvest log, written for this lab and printed in full in Part 4; the garden, its beds, and its harvests are invented and describe no real place or person.
- Rows: 9 short programs in all: 6 programs to trace in the first table and 3 broken programs in the second table
- Language: Python 3, run in a free browser runner that lets a program write and read a small text file; no file to download and nothing to install
- Context: an illustrative community garden with a harvest file of kilograms picked, a file of planting beds, and a weighing step that can meet a bad entry
Part 3 — Materials & Procedure
Tools and materials
- A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting
Ways of working
- write-run-and-verify
- trace-and-predict-output
- find-and-fix-the-bug
Procedure
- Open a free browser Python runner in one tab and Python Tutor at pythontutor.com in another. In the runner, delete any sample code so the editor is blank. Run every program in this lab in the runner, which gives each run its own small folder for files. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: An empty editor with a Run button and a separate output panel.
- In the first table in Part 4, write your prediction for each of the six programs before you run anything. Then type each program into the runner exactly as printed, with four spaces in front of each indented line, press Run, and copy the output into the Actual output column. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: Six actual outputs, one per row, each copied character for character from the output panel; at least one is likely to differ from your prediction.
- Type each broken program from the second table into the runner and press Run. For an error, scroll to the bottom of the error message and copy the error type from its last line. For a program that runs, compare what it printed with what its description says it should print. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: The second table shows what each broken program printed, or the error type on its last line; one of the three runs without any error but prints the wrong number.
- Fix each broken program with the smallest change that makes it do what its description in Part 4 says, run the fixed version, and record the new output. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: Each fixed program runs and prints the harvest total, the skip message, or the number of lines its description asks for.
- Paste the program from row 5 of the first table into Python Tutor, click the button to visualize it, and step forward one line at a time. Watch which line runs right after the line that calls int on the word lots. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: Python Tutor jumps from the int line straight to the except line, skipping the print of kg, and then runs the last line, which prints done.
- Write your own short save-and-reload program: write three garden items of your choice to a file, one per line, then read the file back into a list with strip, print the list and its length, and then open a file that does not exist inside a try with an except that names FileNotFoundError. Predict each printed line, run it, and fill in the third table. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: A short program that runs without an error, with your predicted and actual output side by side.
- Open your approved chatbot, paste the critique prompt from Part 6 as one message, then check each of its claims by running the code it talks about. If you are using the equivalent route, mark up the printed flawed analysis in the same way. Tool: A free browser-based Python runner (an online editor that runs Python 3 with no account and nothing to install), Python Tutor at pythontutor.com for step-by-step visualization, the official Python documentation at docs.python.org, and one approved chatbot used as a pair-programmer whose code the student runs before trusting. Check: The fourth table lists each chatbot claim, what your run showed, and a correction wherever the two disagree.
Fallback route
When to use it: Use this route if your browser Python runner will not open, will not run code, or will not let a program write a file.
Switch to another free browser Python runner and run the same programs there. If no runner will write a file, run rows 5 and 6 and Bug 2 in Python Tutor, which needs no file, and trace the file programs by hand line by line, marking each of those predictions as traced, not run. Complete every table, analysis question, and the critique step, and submit the same items for equal credit.
This route earns the same credit as the primary route.
Part 4 — The Scaffold
Predict first and run second, so the gap between the two columns is your own evidence of what the file holds and which block runs.
| Program | Your prediction | Actual output from the run | Same or different? |
|---|---|---|---|
harvest = [3, 5, 2], then a with open("harvest.txt", "w") as f: block whose loop runs f.write(str(kg) + "\n") for each kg, then with open("harvest.txt") as f: with lines = f.readlines(), then print(len(lines), len(lines[0])) |
|||
harvest = [3, 5, 2], then a with open("harvest.txt", "w") as f: block whose loop runs f.write(str(kg) + "\n") for each kg, then total = 0 and a loop over the reopened file that runs total = total + int(line.strip()), then print(total) |
|||
harvest = [3, 5, 2], then a with open("harvest.txt", "w") as f: block whose loop runs f.write(str(kg) + "\n") for each kg, then text = f.read() on the reopened file, then print(type(text) == str, text.split()) |
|||
beds.txt opened with "w" and given the lines kale and beans, then opened with "w" again and given the line leeks, then print(f.read().split()) on the reopened file |
|||
try: with print("weighing"), kg = int("lots"), and print(kg), then except ValueError: with print("not a number"), then print("done") after the try |
|||
try: with with open("old.txt") as f: and print(f.read()), then except FileNotFoundError: with print("no old log"), run in an empty folder |
Read each broken program from its run before you change a single character.
| Broken program and what it should do | What printed, or the error type on the last line | Your one-change fix | Output after the fix |
|---|---|---|---|
Bug 1, should print the harvest total: write harvest = [3, 5, 2] to harvest.txt one number per line, then total = 0 and a loop over the reopened file that runs total = total + line, then print(total) |
|||
Bug 2, should print skipped for a bad entry: try: with kg = int("n/a"), then except TypeError: with print("skipped") |
|||
Bug 3, should print how many lines the harvest file holds: a loop that runs f.write(str(kg)) for each kg in [3, 5, 2], then print(len(f.readlines())) on the reopened file |
Write your own small save-and-reload program, then let the run confirm or correct what you expected it to print.
| Line you wrote | Output you expected | Output of the run |
|---|---|---|
| Line 1 | ||
| Line 2 | ||
| Line 3 | ||
| Line 4 |
Check every chatbot claim against a run so the run, not the chatbot, has the last word.
| Chatbot claim, quoted | What your run showed | Correct as stated, or your correction |
|---|---|---|
Part 5 — Analysis Questions
- Which of your six predictions in the first table differed from the run? For each one, write the step-by-step trace that reaches the actual output and name the rule that explains the gap, such as a line keeping its newline, a file holding only text, or write mode erasing a file first.
- For row 5 of the first table, which line ran right after the int line in Python Tutor in step 5, and which line never ran? Explain what that tells you about the rest of a try block once an exception is raised.
- Bug 3 runs without any error message but prints the wrong number, while Bugs 1 and 2 stop with an error. Explain why Bug 3 is a logic error, and name the error type each of the other two raised and why the program raised it.
- Choose one: (a) describe how you would decide whether to trust a chatbot's claim about what a file holds, using one claim from your critique as the example; or (b) describe how predicting before running changed what you noticed about newlines and text in this lab, using one row of your first table as the example.
- Every program in this lab wrote and read a few short lines in a fresh folder. Name one thing these runs could not tell you about a save file that is used again and again, such as what happens when a later run finds an old file or a line typed with extra spaces, and say how you would check it by running more cases.
Part 6 — AI-Critique Moment
Choose one equal-credit critique route. Route A: ask an approved AI tool to critique your completed work, then check its response against your own evidence. Route B is the no-AI equivalent printed below.
Approved tools: Claude, Gemini, ChatGPT, Grok, Microsoft Copilot.
Prompt to paste:
I'm doing a beginner Python lab about the harvest log of a community garden, written for a class activity. Please do seven things, and keep each answer short. First, a program writes the text 3 followed by a newline to a file, reads the file back with readlines into a list called lines, and runs print(len(lines[0])). Predict exactly what it prints. Second, a program writes 3 and 5 to a file, each followed by a newline, and then runs print(f.readlines()) on that file. Predict the output and explain in one sentence what readlines does with the end of each line. Third, with lines holding the three lines of the harvest file, write a loop that prints the first two lines. Fourth, this loop stops with an error when one line of the file says n/a: for line in f: with total = total + int(line.strip()). Fix it so the program still prints a total. Fifth, write a program that saves the numbers in harvest = [3, 5, 2] to a file called harvest.txt, one number per line. Sixth, if a program opens a file that already exists with mode w and writes a line, does the new line go after the old lines? Seventh, write code that loads the saved harvest numbers back into a list called loaded.
Test the response for these likely failures:
- Chatbots often predict that
print(len(lines[0]))prints 1, counting only the digit 3. - A chatbot may quote the right list for
print(f.readlines())while explaining that readlines removes the end of each line. - Asked for the first two lines, a chatbot may write
for i in range(1, 3):, which starts at index 1. - A chatbot may fix the crash by wrapping the whole loop in
try:with a bareexcept: pass, which runs without an error. - A chatbot may write
f.write(kg + "\n")with the numbers as they are and call the program working without running it. - A chatbot may claim that mode w adds the new line after the lines a file already holds.
- A chatbot may load the file with a plain
with open("harvest.txt") as f:and miss the first run, when no file exists yet.
Write 2-3 sentences that name one thing the chatbot got right and at least one claim you corrected, and for each correction give the output or error type your own run showed.
Equivalent route (no AI required)
No AI account is required for this lab. Instead of the chatbot, critique this flawed analysis, printed here in full: “After the file is written with the text 3 and a newline, print(len(lines[0])) prints 1, because the line holds one digit. print(f.readlines()) on a file of 3 and 5 gives the two numbers with the line endings removed. To print the first two lines, write for i in range(1, 3): with print(lines[i].strip()). To stop the crash on the n/a line, wrap the whole loop in try: with a bare except: pass; the total is the same as before. To save the harvest, write f.write(kg + "\n") in the loop; this version is working. Opening an existing file with mode w adds the new line after the old ones. To load the log, with open("harvest.txt") as f: is all you need, even on the first run.” Mark it up: quote each flawed sentence, run the code it describes in your browser Python runner, and write the corrected claim beside it with the run's output or error type as evidence. This route assesses the same objectives as the chatbot critique, namely predicting what a file read hands back, converting lines before adding them, catching an error by its type, predicting what write mode does, handling a missing file, and checking every claim about code by running it (Objective 7 and Objective 1), and it takes comparable effort, about 20 minutes of the lab. It is scored by the same rubric for the same credit, and its marked-up analysis replaces the chatbot report and share link in your submission. You do not need to say which route you chose.
Part 7 — What to Submit
- All four completed scaffold tables
- Written answers to all five analysis questions
- Your save-and-reload program with its run output
- The chatbot critique report with its share link, or the equivalent route's marked-up flawed analysis
Deliverable:
A short Coding Lab report on an illustrative community garden's harvest log: four completed tables (six predictions beside their runs, three broken programs read and fixed, your own save-and-reload program, and the checked chatbot claims), written answers to five analysis questions, and a critique of a chatbot's answers to the same questions, or, on the equivalent route, a marked-up correction of the printed flawed analysis. Submit in any accessible file type or as a link; a recorded response may be submitted as audio or video with a transcript, or as text. Choose a written, recorded, or visual form for your answers, such as screen captures of your runs with a short note for each; every form is graded by the same rubric.
Rubric
The rubric below shows how each submitted part is evaluated; bands are full, partial, or none.
| Criterion | Full | Partial | None |
|---|---|---|---|
| Predicting and running the six programs | All six predictions are recorded before the runs, and all six actual outputs match what the programs print, copied character for character, including every value on a line such as 3 2. | The table is mostly complete, but an actual output is missing or miscopied, or predictions appear to have been filled in after running. | No predictions or runs are recorded, or the outputs are invented rather than run. |
| Explaining the gaps between prediction and run | Every prediction that differed from its run is traced step by step to the actual output, with the governing rule named, such as a line keeping its newline, a file holding only text, or write mode erasing a file. | The gaps are listed, but a trace is missing or the rule behind a gap is not named. | No gap is explained, or the response blames the runner instead of the rule. |
| Finding and fixing the three bugs | Each bug is read correctly from its run: what printed or the error type, a one-change fix, and the fixed program's output, with Bug 1 fixed by converting each line with int, Bug 2 by naming the type that is actually raised, and Bug 3 identified as a logic error repaired by writing a newline after each number. | Two of the three bugs are read and fixed correctly, or all three are fixed but an error type or the run evidence is missing. | The bugs are not attempted, or errors are named without running the programs. |
| Writing, running, and verifying a save-and-reload program | The program runs, writes three lines with a newline after each, reads them back into a list with strip, catches the missing file by naming FileNotFoundError, and records its expected and actual outputs side by side. | The program runs but misses one requirement, such as leaving the newlines on the items or using a bare except, or the expected or actual output is not recorded. | No program is submitted, or it was not run. |
| Critiquing the AI analysis or the printed flawed analysis | The chatbot's answers, or on the equivalent route the printed flawed analysis, are quoted and at least two claims are corrected with evidence from the student's own runs. | The critique names errors but corrects only one, or corrects without run evidence, on either route. | No critique is submitted on either route, or chatbot or flawed text is pasted without any checking. |
| Reflection and limitation | The chosen reflection option is answered with a specific example from this lab, and the limitation answer names something these runs cannot show and a sensible way to check it. | The reflection or the limitation is general, without an example from this lab or a way to check it. | The reflection and limitation questions are not answered. |
Quality Gate
- Every prediction in the first table was written before its program was run.
- Every actual output and error type is copied from your own run, never from memory or a chatbot.
- Each error is named by its type from the last line of the error message, not by the wording after the colon.
- Each fix changes as little as possible and its fixed program was run.
- No output, count, or claim is invented anywhere in the submission.
- The critique quotes the chatbot or the flawed analysis and corrects at least two claims with evidence from your runs.
Built with The Course Maker · samples.thecoursemaker.com