← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 10 · Assignment: Traditional alternative (no AI)

Week 10 — The Tally That Lost a Book — Traditional alternative (no AI)

Introduction to Computer Science Free sample edition
Traditional alternative (no AI), unpublished. In the course this is a separate assignment that ships unpublished beside the adaptive assignment: same objective, no AI. An instructor who prefers it publishes it and unpublishes or deletes the adaptive one, as the note at its top says.

Instructors: publish this instead of the adaptive version if you prefer, and unpublish or delete the adaptive one.

Assignment 9 of the term — look things up by name, survive a missing key, count with a dictionary, keep one of each with a set, and explain it all to someone who has never programmed.

This week you learned that a dictionary finds a value by its key, that a missing key raises KeyError while .get() with a default does not, that the counting idiom tallies values in one line, and that a set keeps one copy of each value with no order you can count on. In this assignment you trace four short problems, run each one to check your prediction, run two more programs that show a dictionary's length and the original tally's error, and explain a real mix-up to someone who has never programmed. This builds on Week 9. This week's lecture tutorial, quiz, discussion, and Coding Lab practise the same ideas from other angles.

The task

A neighbor who keeps a little free library box on the street wrote a short program to tally which books people borrowed this week:

borrowed = ["maps", "poems", "maps", "maps", "recipes"]
counts = {}
for title in borrowed:
    counts[title] = counts[title] + 1
print(counts)

It printed nothing and stopped with an error. Your neighbor says, "Python told me my book isn't there, but I was adding it." 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; Problems 1 and 2 use this week's reading log, which maps each book's title to the pages read in it.

Work these four problems. For each one, write your prediction first, then run the code and write the actual output beside your prediction:

  1. The reading log starts as log = {"river book": 120, "atlas": 85}. The program runs log["atlas"] = log["atlas"] + 15 and then print(log["atlas"]). What does the program print?
  2. The updated log is log = {"river book": 120, "atlas": 100, "field notes": 40}. The program sets total = 0, runs for pages in log.values(): with total += pages in the loop body, and then runs print(total). What does the program print?
  3. Your neighbor's loop is fixed to counts[title] = counts.get(title, 0) + 1, and the program then runs print(counts["maps"] + counts["poems"]). What does it print?
  4. The program runs print(len(set(borrowed))) on your neighbor's list. What does it print?

Beside each answer, add one brief interpretation:

  • Problem 1: explain why assigning to "atlas" replaces the value already stored there instead of adding a second entry, then run print(len(log)) on the same log and report what prints.
  • Problem 2: name the order in which the loop visits the three titles, and explain why it visits them in that order.
  • Problem 3: run your neighbor's original version, report what it prints and the exception type on the last line of its error, and explain why .get() with a default of 0 fixes it. Repairing it instead with an in test inside an if statement is equally acceptable.
  • Problem 4: explain why the set is shorter than the list, and why you would not predict the order in which print(set(borrowed)) shows the titles.

Explain it to a non-programmer

Write a 3–5 sentence message to your neighbor that explains, in everyday words, why Python said the book was not there and how changing one line fixes the tally. Use the idea that a dictionary can only look up a name it has already stored, and name one limit or caveat, such as what the fixed tally still cannot tell your neighbor. Explain any technical term in everyday words. More than one good message is possible.

For a different, ungraded example, imagine a cousin whose grocery program turned a shopping list with milk written twice into a set and was surprised by the order it printed in. Model message: "Your program put the list into a set, which works like a hand stamp at the door: each item gets counted once, so the two milks became one. A set makes no promise about order, so the items can come out in a different order than you typed them, and nothing is broken. If you want them alphabetical, ask for the sorted version. One catch: a set forgets how many times you listed something, so keep the original list if the number of cartons matters."

Use that pattern for your neighbor'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 which value each name held or what each lookup found.

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 of print(len(log)) and of your neighbor's original version, each with what printed; and
  • your message to your neighbor, explaining why Python said the book was not there, 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