Back to the Introduction to Computer Science outline The Course Maker
Introduction to Computer Science outline
Week 12 · Coding Lab

Week 12 — Coding Lab / Programming Studio · "Save, Read, Recover"

Introduction to Computer Science · CSCI 1101 Fall 2026 · Prof. Okafor Fictional sample

Course: Introduction to Computer Science — CS1 / Programming Fundamentals in Python (CSCI 1101) · Silver Oak University (fictional sample) · Prof. Okafor
Objective: Objective 7 — read & write files; handle exceptions · SLO A (write & run) + SLO B (trace & debug)
Worth 50 points · Coding Labs group = 15% of the grade · Coding Lab 12
Format: a hands-on programming studio in a free online Python environment — you'll (a) write file code, (b) trace try/except behavior, and (c) find and fix a crash — then catch the AI's mistake.

Everything runs in your browser — nothing to install. The habit: run it and read what Python actually prints (especially the error).


Part 1 — The Big Picture

This week your programs learn to remember (files) and to recover (exceptions). This lab is that, hands-on: write a file and read it back, predict what try/except does, and fix a program that crashes on a missing file.

Your tools: 🔗 https://www.online-python.com/ (write & run) and 🔗 https://pythontutor.com/ (visualize).


Part 2 — Setup (2 minutes)

Open the online Python editor. Run this to confirm files work in your environment:

with open("test.txt", "w") as f:
    f.write("it works")
with open("test.txt") as f:
    print(f.read())

You should see it works.


Part 3 — (a) Write: Save & Read

Write each program, run it, and paste your code + output.
1. Save a list. Write three of your favorite foods to a file (one per line), then read the file back and print each on its own line.
2. Round-trip a number. Write the text "21" to a file, read it back, convert it to an int, and print it doubled. (What does it print? Run it.)
3. Confirm the type. After reading a file with .read(), print type(...) of what you read. What type is it — even when the file holds digits?


Part 4 — (b) Trace: Predict, Then Run

Predict each output first (don't run yet!), then run each and fill in "Actual."

# Program Your prediction Actual
1 write "Python", then print(f.read()) ______ ______
2 write "a\nb\nc\nd", then print(len(f.readlines())) ______ ______
3 try: print(int("12x")) except ValueError: print("bad") ______ ______
4 try: print({"a":1}["z"]) except KeyError: print("missing key") ______ ______
5 try: x = 8/4; print("ok", x) except ZeroDivisionError: print("zero") ______ ______

Hint for #5: does the try block actually fail? And what kind of number does / produce?


Part 5 — (c) Find & Fix the Bug

This program crashes when settings.txt doesn't exist. Run it (it will raise an exception), then write (i) the exception type, (ii) why, and (iii) a fixed version that prints Using default settings. instead of crashing.

with open("settings.txt") as f:
    print(f.read())

Part 6 — Analysis Questions

  1. In the trace table, which prediction did you get wrong (if any)? What did you learn?
  2. In #3 and #4, the try blocks failed but the program didn't crash. Why not?
  3. In #5, the except block never ran. Why?
  4. Why is with open(...) better than calling open(...) and f.close() yourself?
  5. Connect it: a file read always returns a string. Why does that mean you often pair file reading with a try / except ValueError?

Part 7 — AI-Critique Moment (required — this is the BYOAI step)

Bring in your approved chatbot and check its work.
1. Ask it: "Write a Python program that reads a number from a file score.txt and prints it doubled, handling the case where the file is missing OR holds non-numeric text."
2. Run its code and check: Did it catch both FileNotFoundError and ValueError (the two real failure modes)? Or did it use a single bare except: that hides which thing went wrong? Did it remember that the read returns a string that needs int(...)?
3. Write 2–3 sentences on what it got right and one thing you had to correct or verify by running it.

The habit: the tool drafts, you run it and judge. Chatbots routinely catch the wrong exception type or use a too-broad except — catching that is the point.


Part 8 — What to Submit

Your Part 3 programs + outputs; your completed Part 4 trace table; your Part 5 bug answers (type, why, fix); your Part 6 answers; and your Part 7 AI-critique paragraph. Due Sunday, Nov 22, 11:59 p.m. (50 points).


Instructor answer key & model outputs — REMOVE BEFORE PUBLISHING TO STUDENTS

Execution gate: PASS — every output below was produced by running the code.

Part 3 (model): (1) three foods on three lines; (2) writes "21", reads, int(), prints 42; (3) type(f.read())<class 'str'> (a read is always a string, even for digits).

Part 4 trace table (run-verified):

# Actual output
1 Python
2 4
3 bad
4 missing key
5 ok 2.0

Part 5 bug (run-verified): (i) FileNotFoundError; (ii) settings.txt doesn't exist, so open can't find it; (iii) fix:

try:
    with open("settings.txt") as f:
        print(f.read())
except FileNotFoundError:
    print("Using default settings.")

→ prints Using default settings. when the file is missing.

Part 6 (expected): (1) commonly #5 (ok 2.0, not 2). (2) The try/except caught the raised exception (ValueError, KeyError) and ran the except instead of crashing. (3) The try block succeeded (8/4 is fine), so the except was skipped. (4) with auto-closes the file even if an error occurs — you can't forget f.close(). (5) A read is a string, so converting it with int(...)/float(...) can raise a ValueError if the text isn't a valid number — hence the try/except ValueError.

Part 7: full credit for a specific catch — most commonly the AI catching only one of the two failure modes, using a bare except:, or forgetting the int(...) conversion.

Grading rubric — 50 points

Criterion Full Partial None
Part 3 — wrote & ran 3 file programs (code + output; #2 doubles via int, #3 shows str) (14) 14 7–11 0–5
Part 4 — trace table (predictions attempted + all 5 actual outputs correct from running) (14) 14 7–11 0–5
Part 5 — found & fixed the bug (type + why + working try/except FileNotFoundError) (12) 12 6–10 0–4
Part 6 — analysis (why no crash; why except skipped; why with; string→ValueError) (6) 6 3–5 0–2
Part 7 — AI-critique (names a specific thing checked/corrected by running) (4) 4 2 0–1

Quality gate (self-checked): every model output (42, <class 'str'>, Python, 4, bad, missing key, ok 2.0, Using default settings.) and every exception type was produced by running the codeexecution gate: PASS.

~ Prof. Okafor's edition · Fall 2026 · built with thecoursemaker.com