Skip to course content
Free course

Python Foundations / Module 4 / Applied Checkpoint: Validate Study Records

Module 4 lesson

Applied Checkpoint: Validate Study Records

Unit ID: M04-U09 Estimated active time: 45-60 minutes

Supplied data

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

Use only these fictional records.

Rules

A record is accepted only when:

  • id exists, has exact type str, and contains non-whitespace text;
  • hours exists, has exact type int, and is from 0 through 40 inclusive.

Required outputs

Create:

  • accepted_records: copies of valid records;
  • rejected_records: dictionaries containing the original position, a safe display ID, and a list of reasons;
  • accepted_hours_total;
  • accepted and rejected counts; and
  • has_rejections boolean.

Do not overwrite or remove items from records.

Required process

  1. Write the plain-language algorithm before coding.
  2. Initialise all output state before the loop.
  3. Use enumerate(records, start=1).
  4. Create a fresh reasons list for each record.
  5. Validate ID and hours separately.
  6. Use elif so range comparison occurs only after integer type passes.
  7. Append a record copy only when reasons is empty.
  8. Otherwise, append all reasons to rejected output.
  9. Calculate summary values after or during the loop without double counting.

Expected result

  • Accepted IDs: R1, R5
  • Accepted count: 2
  • Rejected count: 5
  • Accepted hours total: 48
  • Rejections exist: True

Required assertions

Assert the expected counts and total. Also assert that records still equals a preserved source copy.

Self-review rubric

CriterionNot yetMeets
AlgorithmCoding begins without clear rules and outputsInputs, outputs, repetition, decisions, and boundaries are stated first
Type and rangeBooleans or numeric text are accepted accidentallyExact integer type and inclusive range are enforced
Rejection evidenceInvalid records disappear or only one generic reason appearsEvery rejection keeps position, display ID, and all relevant reasons
Source preservationSource records are changed or removedAccepted records are copies and source equality is asserted
StateCounts or totals depend on stale notebook valuesState is initialised once and updated in the correct branch
Edge casesMissing keys or spaces-only IDs fail unexpectedlyMissing, blank, boundary, string, boolean, and negative cases are handled
ReproducibilityClean rerun failsRestart-and-run-all passes all assertions