Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
Say the steps before opening the answer: clean the line, split it, convert the number, handle failure, store the record, then report it.
Show the answer
strip() → split() → int() inside try → dictionary or None → loop and write.
A first project with an explicit data policy
Trace, rebuild, fill and repair the demonstration cleaner, then apply the ideas to a new independent report. The demonstration accepts a simple two-field input under limited assumptions; it is not a general production validator.
The independent project tightens the policy: exactly two fields, nonempty trimmed name and nonnegative integer frames. Valid zero remains accepted. Count accepted and skipped rows explicitly, preserve order and replace the report on every call.
Plan two sessions: the original four practice tasks take about 51 minutes; the independent project adds about 20 minutes. The scored quiz is separate. Stop between sessions and keep a code or report copy.
# A first project with an explicit data policy.
rows = [" Museum | 0 ", "Bad|many", "Canal|7"]
accepted = 0
skipped = 0
for row in rows:
parts = row.strip().split("|")
try:
frames = int(parts[1])
except (ValueError, IndexError):
skipped += 1
continue
accepted += 1
print(parts[0].strip(), "·", frames, "frames")
print("Accepted:", accepted, "Skipped:", skipped)
Museum · 0 frames, Canal · 7 frames, then Accepted: 2 Skipped: 1. This worked trace only covers the demonstration assumptions; it does not implement the full independent project.
Use the independent brief for the new rules. Check the saved file and counts on unfamiliar rows and a second call; quiz recall alone is not practical mastery.
Predict, explain, then verify
These brief checks are self-reviewed, not quiz scores. Browser runs use fresh temporary files; they do not open your computer’s files. Download useful reports before another Run, Reset, exercise change or leaving.
Keep valid zero
Should a record with frames 0 be counted as rejected under the new policy?
Compare your answer · self-reviewed
No. Zero is a valid nonnegative integer, distinct from a malformed row.
Avoid hidden carry-over
What should a second write_report([]) call leave in the report file?
Compare your answer · self-reviewed
An empty file and zero accepted/skipped counts. The function replaces rather than appends.
Explain a rejection
Why reject Too|many|fields instead of silently reading the first two fields?
Compare your answer · self-reviewed
The independent policy requires exactly two fields. Silently discarding fields hides malformed data.
Independent project: a trustworthy report
Write write_report(lines) for a list of strings in the simple name|frames format. This new policy is stricter than the demonstration cleaner:
- Accept exactly two trimmed fields, a nonempty name and an integer frame count of zero or more.
- Skip malformed strings: empty names, bad numbers, negative frames and missing or extra fields.
- Preserve accepted row order and duplicates. Do not modify the input list.
- Replace
project-report.txton each call, using UTF-8. Writename · N framesplus a newline per accepted row. If none qualify, create an empty file. - Return a dictionary containing exactly
acceptedandskipped, both integer counts. Count each input row once. - Try empty input, valid zero, rejected rows around valid rows, then a second call. Catch
ValueErrorclose to numeric conversion; do not hide unrelated problems.
This task adds about 20 minutes after the four original practice steps. Use two sessions if needed. The checkpoint link selects the unfinished starter; it does not run Python or award a pass. Review writing and reading a report.
Read the unfinished starter without running Python
# Build a report under the new policy in the brief.
def write_report(lines):
# Replace this unfinished return with your workflow.
return {"accepted": 0, "skipped": 0}
# Run the sample, then inspect the saved report.
rows = [" Museum | 0 ", "Broken", "Canal|7", "Bad|-1"]
result = write_report(rows)
print("Accepted:", result["accepted"])
print("Skipped:", result["skipped"])
with open("project-report.txt", encoding="utf-8") as file:
print(file.read(), end="")
The sample accepts Museum with zero frames and Canal with seven; Broken and Bad|-1 are skipped. Expect Accepted: 2 and Skipped: 2, followed by the two report lines. Downloading this report or passing the quiz does not replace the independent code checks.
Choose an exercise to load its prompt.
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
Section 4 checkpoint
Answer five questions, review explanations and revisit the linked lessons. Your score is separate from the independent report check. Tutor login is not required.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.