Open lesson

Mission: protect the three-batch report

Command palette

This mission workspace keeps code and notes in this browser tab. Export them before closing it. Running code is practice, not an automatic grade for the mission.

Brief

Validate input, reject an impossible value with a reason, and let the program continue without inventing a replacement.

Imani receives three batches before an evidence handoff. A claims 5 rows and 4 distinct IDs; B claims 3 rows and 4 IDs; C claims -1 row and 0 IDs. Voss wants all three displayed as numeric results. Mara asks you to apply validation and preserve rejection reasons.

Your mission

  1. For A, B, and C, say whether the function returns a number or raises ValueError.
  2. Calculate repeated rows only for valid batches.
  3. Give the exact reason for B and C, and explain the order of the two checks.
  4. Say what Imani should keep to trace the cause, without replacing B or C with zero.

How to check your work

  • A gives 1; B and C are rejected.
  • You calculate neither -1 for B nor any valid result for C.
  • B has “distinct exceeds rows”; C has “negative count” because that check comes first.
  • You keep raw values, source, date, and rejection reasons.

Course example

def repetitions(rows, distinct):
    if rows < 0 or distinct < 0:
        raise ValueError('negative count')
    if distinct > rows:
        raise ValueError('distinct exceeds rows')
    return rows - distinct

try:
    result = repetitions(3, 4)
    print(result)
except ValueError as error:
    print(f'rejected: {error}')

Code

Use Tab to leave the editor. Ctrl+Enter runs the source. The lines beginning with # are task reminders. Python ignores them. Write your code beneath them.

Python console

Loading laboratory…

Explore Python or SQL here; this is not an operating-system shell.

This is a disposable browser experiment. The code run here is not recorded as a completed mission.

Each command starts a fresh disposable worker: it runs a captured copy of your current source, then your command with the lesson fixtures. Changes are discarded afterward. Commands never save source or award XP. Ctrl+Enter executes the command.

Virtual workspace files

Source and declarative assets are readable here without JavaScript. Python input files are created at runtime; SQL setup initializes the disposable database.

source.py

Current editor source. Inspection shows your current edits.

# For A, B, and C, say whether the function returns a number or raises `ValueError`.
# Calculate repeated rows only for valid batches.
# Give the exact reason for B and C, and explain the order of the two checks.
# Say what Imani should keep to trace the cause, without replacing B or C with zero.

exemple.py

Declarative lesson asset; this is not a file created in the Python filesystem.

def repetitions(rows, distinct):
    if rows < 0 or distinct < 0:
        raise ValueError('negative count')
    if distinct > rows:
        raise ValueError('distinct exceeds rows')
    return rows - distinct

try:
    result = repetitions(3, 4)
    print(result)
except ValueError as error:
    print(f'rejected: {error}')