finally, with, assert

finally always runs — close the file. with open(...) as f does that for you. assert is a check for developers, not a user-facing KYC message.

!

Broken form input

Check values before you calculate.

You will be able to

  • Know finally always runs — close the file whether or not you caught an error
  • See with as the laptop spelling that does that close for you
  • Treat assert as a developer check, not a user-facing KYC message

Cleanup is not optional

You opened attendance.csv. A later line may fail. finally: on a laptop runs after try, whether except happened or not — the place to close the handle or print Cleanup so a technician knows the lock was released. with open(...) as f: is the shorter spelling: enter, run the block, exit (close) even if the block raised. This sandbox cannot open files. You will still print Open, Work, Cleanup in that order so the habit is in your fingers.

forgetting Cleanup is how a nightly job leaves a 0-byte file and a locked USB. The status string is not the file; it is the policy.

Laptop: finally, with, assert

path = "attendance.csv"
try:
    f = open(path, encoding="utf-8")
    text = f.read()
finally:
    f.close()

with open(path, encoding="utf-8") as f:
    text = f.read()

def free_seats(used, total):
    assert total > 0
    return total - used

assert is for writers, not for clerks

assert seats > 0 on a laptop is a check that your own function was called correctly. If it fails, the program is allowed to crash with AssertionError — you have a bug. Do not assert a student’s PIN and show the traceback at the kiosk. KYC refusals are Reject messages you designed. assert can be stripped with python -O; user policy cannot.

Use assert in tests and in “this list of rolls must already be unique” inside a developer-only path. Use OK/Reject for the form.

Words that matter

finally
Laptop block that always runs after try — close, unlock, Cleanup.
with
Laptop context manager — open and guaranteed close.
assert
Developer check; not a KYC sentence for a student.

Common mistakes

Avoid: Showing AssertionError to a student who typed a 5-digit PIN.

Do this: Reject at the edge; assert for your own preconditions.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — Cleanup always prints

Work can succeed. Cleanup still runs. Order: Open, Work, Cleanup.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — Cleanup even after Reject

Short PIN is Reject. Cleanup still prints.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

On a full Python install — with / finally

with open(...) as f closes the handle. finally is the same idea for anything you must always run.

stdlib — no pip

Cleanup that survives a failure.

Real library code (not run in this browser sandbox)

def parse_seats(text):
    n = int(text)
    assert n >= 0
    return n

try:
    parse_seats("40")
finally:
    print("Cleanup")

Example program — Developer check as a flag, user message as Reject

total 0 is a programmer bug. Negative used is a form Reject.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Always Cleanup

Print Work then Cleanup (Cleanup must appear even if you add a Reject line).

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. finally is for…
2. assert should…

Progress is stored in a browser cookie on this device.