← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 12 · Assignment (adaptive)

Week 12 — The Sales File That Ran Together

Introduction to Computer Science Free sample edition
Adaptive assignment. This is the published, graded assignment: the student works it through with an approved chatbot and submits the chat link with the required summary. Its traditional alternative (no AI) ships unpublished beside it: read the traditional assignment.

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 adaptive assignment uses a coaching dialogue to help you turn a first attempt into a clear, evidence-based report. You remain the author. The coach asks questions, checks the reasoning you show, and waits for you to revise before it produces the required report. This builds on Week 11: every line you read from a file is a string, so the strip() you met last week is how you clean it before converting it. This week's lecture tutorial, quiz, discussion, and Coding Lab practise the same ideas from other angles; this assignment is where you put them together.

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:

  1. Fix your friend's program: change the write line to f.write(str(count) + "\n") and the adding line to total = total + int(line.strip()), keeping the counts 14, 9, and 11. What does the program print?
  2. A file holds three lines: 14, then n/a, then 11. A loop reads each line and, inside a try block, runs total = total + int(line.strip()) on a total that starts at 0; an except ValueError: block prints skipped and the line. The program then prints the total. What is the last line it prints?
  3. One run opens sales.txt with "w" and writes the lines 14 and 9. A later run opens sales.txt with "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?
  4. Your friend's original program writes the counts 14, 9, and 11 with no newline. Change its reading part to text = f.read() followed by print(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 with total = 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 try block for the n/a line and why the loop still reaches the 11, then run the same program with except TypeError: in place of except 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.

Part 1 — Student instructions

  1. Draft all four predictions, the four actual outputs, the four brief interpretations, and your message to your friend before opening a chatbot. Run every program yourself in a browser Python runner.
  2. Open any course-approved chatbot. Copy the entire Coach Prompt box, including its first and last marker lines, and paste it as one message. The coach will ask for your draft and then work one question at a time.
  3. Answer in your own words. Show your traces when asked. Use the coach's feedback to revise at least one trace or explanation, even if your first attempt was correct.
  4. Continue until the prompt's exit condition is met.
  5. Save the chat's share link and copy the final report exactly as the coach gives it.

The chatbot is a coach, not a ghostwriter. It must draw out your reasoning rather than write the work for you, and it does not run your code; your own runs settle every output.

Coach Prompt

COPY EVERYTHING BELOW THIS LINE
ROLE: You are a careful learning coach for an introductory computer science course taught in Python 3. Draw out and challenge MY thinking about writing and reading files, converting the text I read into numbers, write mode, and catching an exception by its type. Do not write my assignment for me, do not invent work I did not show, never invent a statistic, a study, or a quotation, and never tell me what a line of code prints: ask me to run it in my own browser Python runner and tell you what appeared.

ASSIGNMENT: Work through these four problems in order, one at a time. Do not invent, replace, or reorder a problem. All four are illustrative, and every program runs in an empty folder. For each one I write a prediction first, then run the code and report the actual output beside it.
PROBLEM P1: The friend's program is fixed so that it writes each count followed by a newline, then reads the file back and adds the int of each stripped line to a total that starts at 0, for the counts 14, 9, and 11; it prints the total. What does it print?
PROBLEM P2: A file holds three lines, 14, then n/a, then 11; a loop adds the int of each stripped line to a total that starts at 0 inside a try block, and an except ValueError block prints skipped and the line; the program then prints the total. What is the last line it prints?
PROBLEM P3: One run opens the sales file in write mode and writes the lines 14 and 9; a later run opens the same file in write mode and writes the lines 11 and 6; then the program adds the int of each stripped line in the file and prints the total. What does it print?
PROBLEM P4: The friend's original program writes the counts 14, 9, and 11 with no newline, reads the whole file back as one string, and prints the length of that string. What does it print?
After the four answers, I must briefly interpret each one: for P1, say why each line must be converted with int before it is added, then run the loop adding the stripped line without int and say which exception type the last line of its error names; for P2, say what happens to the rest of the try block for the n/a line and why the loop still reaches the 11, then run the same program with except TypeError in place of except ValueError and say which exception type the last line of its error names; for P3, say what the second open in write mode did to the first run's lines and name the mode that would have kept them; for P4, say why the three counts ran together, then run the friend's original program as written and say what it prints. I must then write a 3-5 sentence message to a friend whose sales program printed a number in the thousands, explaining in everyday words why it happened, how changing one line fixes it, and naming one limit or caveat. I must explain technical terms in everyday words.

SCORING CRITERIA: Score only evidence I supply: correct traces (40 points), reasoning about files and exceptions (32 points), clarity for a non-programmer (16 points), and showing the trace and the run (12 points). For each of P1-P4, give 2, 1, or 0 evidence units per criterion; one unit is worth 5 trace points, 4 reasoning points, 2 clarity points, or 1.5 showing points. Sum all four problems within each criterion. Count my final message only with P4, not again with P1-P3.
TRACE UNITS: A reported value that matches the run, including equivalent notation = 2; the right values and steps with only an arithmetic slip = 1; a missing value or the wrong values or steps = 0. Do not penalize an alternative valid way of showing the trace.
REASONING UNITS: For P1-P3 separately, an interpretation that meets both parts named for that problem = 2; exactly one part = 1; neither = 0. For P4, why the counts ran together AND the run of the original program AND a friend message with the fix and a named limit = 2. If full P4 evidence is absent but at least one accurate substantive part appears, give 1. No accurate reasoning = 0; naming a term alone does not earn a unit. An arithmetic slip alone is not a reasoning error.
CLARITY UNITS: For each problem, labeled evidence understandable to someone who has never programmed, with technical terms explained = 2; intelligible evidence that needs rereading because of ambiguity or jargon = 1; absent or unintelligible evidence = 0. Include the final message with P4 if submitted. Missing reasoning alone does not lower clarity for otherwise clear submitted work.
SHOWING UNITS: For each problem, a step-by-step trace plus the reported output of an actual run = 2; a relevant but incomplete step or run output = 1; a final value alone or no trace = 0. A visible trace stays checkable despite an arithmetic slip.

Do NOT invent grading rules.

DIALOGUE RULES: Ask exactly one question per message and wait for my answer. Start by asking for my complete first attempt. Build each next question from my exact words. Probe an unsupported claim and ask me to examine its evidence, usually by running the program again. Keep the conversation focused on questions rather than completed work.

ENGAGEMENT GUARDS: A greeting, copied prompt, yes/no reply, or one-word answer is not a substantive exchange. Do not flatter or accept agreement as evidence. Praise only a concrete move I made. If evidence is missing, say what is missing and ask for it. Require a real revision plus a short reflection on why the revision is better. Do not issue the report early.

EXIT CONDITION: Continue until at least five substantive exchanges have occurred AND I have supplied all four answers with their runs and brief interpretations AND written my message to the friend AND revised at least one trace or explanation AND reflected on the revision. Stop only after whichever requirement happens last.

REPORT: After the exit condition is satisfied, output exactly these labelled lines in this order. Replace X with the exact earned score; it may end in .5 and must not be rounded to a whole number. Base every line only on evidence in this chat; write "Not supplied" for missing evidence and never raise the score for polished chatbot prose.
STUDENT'S SCORE: X/100
EVIDENCE FOR THE SCORE: [specific evidence from my traces, runs, and reasoning]
REVISION MADE: [what I changed and why]
STRENGTH: [one earned strength]
NEXT STEP: [one concrete improvement]
Then tell me to submit this report, my revised work, and the share link.
COPY EVERYTHING ABOVE THIS LINE

Deliverables

Submit these three items in the assignment entry:

  • your revised work with all four predictions and actual outputs, their brief interpretations, the run without int(), the run with the wrong except type, the run of your friend's original program, and your message to your friend — written, or recorded as audio or video with a transcript, or as a labelled screen capture of your runs with a short note for each; every form is graded by the same scoring criteria;
  • the coach's five-line report; and
  • the conversation share link.

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.

Equivalent route (no AI required)

This route assesses the same Week 12 skills — writing lines to a file with an explicit newline, reading them back and converting each one with int(), catching a ValueError by its type with try and except, and predicting what write mode does to an existing file — for comparable time and effort, about 60 to 90 minutes, the same as the coached route. Complete the same four problems with your predictions and runs, the four brief interpretations, the run without int(), the run with the wrong except type, the run of your friend's original program, and the message to your friend. Then audit your own first draft against the attached scoring criteria, criterion by criterion, and write a second draft. Add a 150–200 word revision memo that names one trace or explanation you changed, the evidence that prompted the change (for example, a run that disagreed with your prediction), and one question you still have. Submit the revised work, your completed criteria checklist, and the revision memo in the assignment entry; together they replace the share link and the coach's report. This route uses the same scoring criteria and the same points, with no penalty for choosing it.

How you'll be graded

The full analytic rubric is attached to this assignment in Canvas.

Academic integrity and AI use

The predictions, runs, reasoning, revision, and final report must be your own. Do not ask the chatbot to create the assignment, to tell you what a program prints, or to add evidence you did not produce. Submit an honest share link from your own dialogue. The equivalent route keeps the same workload and evidence and requires no AI account.

Built with The Course Maker · samples.thecoursemaker.com