EE1021 Programming for Engineers · Lecture 6 Lab · 2026–2027
By the end of the lab, you should be able to:
For every defect, record:
| Input | Expected | Actual evidence | Hypothesis | Fix | Retest |
|---|
Do not edit until you can state a hypothesis supported by evidence.
Open starter/lecture6_lab.py and keep the supplied function names unchanged.
For an input of 20, the expected result is 68. The function returns 4.
Check: logic error; change - 32 to + 32, then test 20, 0, and a negative value.
Open starter/stage1_syntax_bug.txt.
Expected classifications: alarm and normal.
Show your partner:
SyntaxError message;State why exception handling would not be the appropriate repair.
Call parse_reading with:
For each record, write the expected behavior before running. Record the exception type, message, and failing operation for invalid input.
The repaired function must:
(sensor_id, numeric_value) for SENSOR,VALUE;ValueError("expected SENSOR,VALUE") for the wrong number of fields;ValueError("value must be numeric") when conversion fails;Use validation for structure and a narrow try block for numeric conversion.
Where should the try block begin and end?
Place only float(value_text) in the try block. Structure validation is clearer before conversion, and unrelated statements should remain visible if they fail.
Decide which contract applies:
KeyError reveals invalid program data.Implement option 3 in classify_reading without catching unrelated exceptions.
Required results:
classify_reading("T1", 70.0, limits) # "normal"
classify_reading("T1", 85.0, limits) # "alarm"
classify_reading("T9", 70.0, limits) # "unknown"Do not use a default limit that silently classifies an unknown sensor as normal.
Call average_reading([]) and read the traceback from the final line upward.
The contract for this lab is:
Return the average of a non-empty sequence; raise
ValueErrorfor an empty sequence.
Repair the function without catching ZeroDivisionError after it happens.
Why is an explicit empty-sequence check clearer here than catching ZeroDivisionError?
The contract directly forbids empty input. Validation names that condition and avoids confusing it with another possible division defect.
The current function divides by len(values) - 1.
Use this evidence table before editing:
| Input | Expected |
|---|---|
[20.0] |
20.0 |
[20.0, 22.0, 24.0] |
22.0 |
[] |
ValueError |
Repair the formula and retain all three cases as regression tests.
Search your file for:
except:;except Exception:;pass;try blocks containing unrelated operations.Replace each with a specific design or let the unexpected exception propagate.
Implement process_records(records, limits) so it:
(accepted, errors);The starter includes starter/sensor_readings.txt as optional data.
Your test set must include:
average_reading.Every repaired defect needs a test that would fail if the defect returned.
For one repaired function, explain:
Each student independently completes one action:
try block;ValueError for a violated contract;You may retry once after focused feedback.
except, silent pass, or broad except Exception remains.ValueError.process_records are explained.