Week 12 — The Sales File That Ran Together — Traditional alternative (no AI)
Instructors: publish this instead of the adaptive version if you prefer, and unpublish or delete the adaptive one.
Assignment 11 of the term — save numbers to a file and read them back as numbers, skip a bad line by its error type, see what write mode erases, and explain it all to someone who has never programmed.
This week you learned that a file holds only text, that write() adds nothing after the string you hand it, that int() turns a line you read into a number, that write mode erases an existing file before writing, and that an except block catches only the exception type it names. In this assignment you trace four short problems, run each one to check your prediction, run three more programs that show what goes missing when a step is skipped, and explain a real mix-up to someone who has never programmed. This builds on Week 11. This week's lecture tutorial, quiz, discussion, and Coding Lab practise the same ideas from other angles.
The task
A friend who sells potted plants at a weekend market stall wrote a short program that saves each day's plant sales to a file and then reads the file back to add them up:
sales = [14, 9, 11]
with open("sales.txt", "w") as f:
for count in sales:
f.write(str(count))
total = 0
with open("sales.txt") as f:
for line in f:
total = total + int(line)
print(total)
It ran without an error, but it printed a number in the thousands. Your friend says, "I sold a few dozen plants, not thousands. Where did that number come from?" Your job is to check what Python really did, the way a programmer would: predict each output before you run it, run it in a free browser Python runner, and explain what happened. All four problems are illustrative; run every program in an empty folder, so that no older file gets in the way.
Work these four problems. For each one, write your prediction first, then run the code and write the actual output beside your prediction:
- Fix your friend's program: change the write line to
f.write(str(count) + "\n")and the adding line tototal = total + int(line.strip()), keeping the counts 14, 9, and 11. What does the program print? - A file holds three lines: 14, then
n/a, then 11. A loop reads each line and, inside atryblock, runstotal = total + int(line.strip())on a total that starts at 0; anexcept ValueError:block printsskippedand the line. The program then prints the total. What is the last line it prints? - One run opens
sales.txtwith"w"and writes the lines 14 and 9. A later run openssales.txtwith"w"again and writes the lines 11 and 6. The program then reads the file back, adds the int of each stripped line to a total that starts at 0, and prints the total. What does it print? - Your friend's original program writes the counts 14, 9, and 11 with no newline. Change its reading part to
text = f.read()followed byprint(len(text)). What does it print?
Beside each answer, add one brief interpretation:
- Problem 1: explain why each line must be converted with
int()before it is added, then run the loop withtotal = total + line.strip()in place of the conversion and report the exception type on the last line of its error. - Problem 2: explain what happens to the rest of the
tryblock for then/aline and why the loop still reaches the 11, then run the same program withexcept TypeError:in place ofexcept ValueError:and report the exception type on the last line of its error. - Problem 3: explain what the second open in write mode did to the first run's lines, and name the mode that would have kept them.
- Problem 4: explain why the three counts ran together, then run your friend's original program as written and report what it prints.
Explain it to a non-programmer
Write a 3–5 sentence message to your friend that explains, in everyday words, why the program printed a number in the thousands and how changing one line fixes it. Use the idea that a file holds only text, and that write() puts down exactly what you hand it and nothing more. Name one limit or caveat, such as what the fixed program still does if a line holds a word instead of a number. Explain any technical term in everyday words. More than one good message is possible.
For a different, ungraded example, imagine a cousin whose to-do app opened its save file in write mode every morning and kept losing the list from the day before. Model message: "Your app opens its save file in write mode, which works like a whiteboard that gets wiped clean before anyone writes on it: everything from the day before is erased first. So each morning the file holds only what you added that day. Opening it in append mode instead keeps the old lines and adds the new ones at the end. One catch: append mode never removes anything, so finished tasks will pile up unless the app rewrites the list on purpose."
Use that pattern for your friend's question, with your own wording and evidence.
Show your traces. A reader should be able to follow every prediction step by step and see the actual output of your run beside it, without guessing what the file held or which block ran.
Deliverables
Submit a single report containing:
- the four answers, each with your prediction, the step-by-step trace, the actual output of your run, and the brief interpretation the task names for that problem;
- the runs without
int(), with the wrongexcepttype, and of your friend's original program, each with what printed or the exception type on its last line; and - your message to your friend, explaining why the program printed a number in the thousands, how changing one line fixes it, and one limit.
Keep it to roughly one page. Clear and correct beats long.
Choose how you show it. Present your report as written text, or as a recorded audio or video explanation of about three minutes with a transcript, or as labelled screen captures of your runs with a short note for each. Every form is graded by the same scoring criteria. 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.
How you'll be graded
The full analytic rubric is attached to this assignment in Canvas.
Academic integrity and AI use
This version is designed to be completed without AI: make the predictions, run the code, and write the interpretations and the message yourself, without any AI tool. You may use a free browser Python runner, Python Tutor, and the official Python documentation. Submitting AI-generated work as your own, or copying another student's work, is an integrity violation under the course policy.
Built with The Course Maker · samples.thecoursemaker.com