Skip to course content
Free course

Python Foundations / Module 6 / Applied Checkpoint: Repair an Hours-Processing Program

Module 6 lesson

Applied Checkpoint: Repair an Hours-Processing Program

Unit ID: M06-U08 Estimated active time: 50-70 minutes

Broken program

The supplied program assumes every hours value is numeric text, stops at the first bad record, silently accepts some unsuitable values, and has no focused tests.

Use these fictional records:

records = [
    {"id": "R1", "hours": " 8 "},
    {"id": "R2", "hours": 12},
    {"id": "R3", "hours": "six"},
    {"id": "R4", "hours": -2},
    {"id": "R5", "hours": True},
    {"id": "R6"},
    {"id": "R7", "hours": "40"},
]

Required diagnosis

Before repair, record:

  • expected accepted records;
  • the first actual failure and traceback;
  • the smallest reproducing input;
  • every violated assumption you find; and
  • why a broad catch or silent conversion would be unsafe.

Required repair

Create parse_hours(raw_hours) with this contract:

  • accept exact integers and integer text;
  • strip outside text whitespace;
  • reject booleans, floats, None, malformed text, and empty text;
  • require 0 through 40 inclusive;
  • raise TypeError for unsupported types;
  • raise ValueError for malformed or out-of-range values.

Create process_records(records) that:

  • preserves the source list and dictionaries;
  • continues after expected KeyError, TypeError, or ValueError per record;
  • returns accepted record copies, rejected details, and accepted-hours total;
  • records position, safe ID, exception type, and reason for each rejection.

Expected outcome

  • Accepted IDs: R1, R2, R7
  • Accepted hours: 8, 12, 40
  • Accepted total: 60
  • Rejected count: 4

Required tests

Test:

  • normal integer and numeric text;
  • 0 and 40;
  • -1 and 41;
  • True, None, and 3.5;
  • "six", "8.5", and empty text;
  • missing hours key in record processing;
  • source preservation; and
  • expected accepted IDs, rejected count, and total.

Self-review rubric

CriterionNot yetMeets
DiagnosisCode is changed before evidence is recordedExpected, actual, traceback, minimal input, and violated assumptions are documented
ValidationValues are silently coerced or booleans passType, text, and range rules match the contract
ExceptionsFailures are swallowed or caught broadlySpecific errors have useful messages and expected catches are narrow
ProcessingFirst invalid record stops the workflowAll records produce accepted or rejected evidence
Source safetyInput records are changedSource equality is tested and preserved
TestsOnly one successful example is triedNormal, boundary, invalid, missing, and integration cases pass
ExplanationRepair is unexplainedRoot cause, repair, and residual limits are stated clearly