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
- 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.
How to check your work
- A gives 1; B and C are rejected.
- You calculate neither
-1for 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}')