EE1021 Programming for Engineers · Lecture 5 Lab · 2026–2027
By the end of the lab, you should be able to:
For every task:
Open starter/lecture5_lab.py and keep the supplied names unchanged.
readings = {"S1": 4.92, "S2": 5.08}
readings["S2"] = 5.10
readings["S3"] = 4.97
print(len(readings), readings["S2"])Predict:
Check: S2 is replaced; 3 5.1 is printed; a missing direct lookup raises KeyError.
Create:
Print:
Record predictions before running.
Perform these operations in order:
Print the dictionary after each operation and print both returned values.
Show your partner, then the instructor if requested:
Complete:
def average_reading(readings):
"""Return the average of a non-empty reading dictionary."""
total = 0
for value in readings.values():
total += value
return total / len(readings)Test with {“A”: 1.0, “B”: 2.0, “C”: 3.0}. The expected result is 2.0.
Implement:
Then iterate over readings.items() and build:
Do not request input inside either function.
Build a second dictionary mapping status names to counts.
status_counts = {}
for status in statuses.values():
status_counts[status] = status_counts.get(status, 0) + 1Print the result and verify that the sum of its values equals the number of sensors.
After each iteration, record the complete dictionary state.
Check: {“alarm”: 1}; then {“alarm”: 1, “normal”: 1}; finally {“alarm”: 2, “normal”: 1}.
registered = set(readings)
active = {"S1", "S2", "S4", "S5"}
calibrated = {"S1", "S4"}
faulty = {"S2", "S5"}Explain why set(readings) contains sensor IDs rather than reading values.
Confirm that empty braces create a dictionary and set() creates an empty set.
Write one set expression for each:
Print each result with a clear label.
Draw or explain the result of one instructor-selected expression:
State what the result means in words before running it.
Implement:
def fleet_report(readings, calibrated):
"""Return a dictionary containing summary information."""
...Return a dictionary containing:
Use your earlier functions instead of repeating their logic.
The starter folder includes sensor_events.txt.
with open("sensor_events.txt", encoding="utf-8") as file:
events = file.read().split()
event_counts = {}
for event in events:
event_counts[event] = event_counts.get(event, 0) + 1Move the Python file and text file together before running this extension.
Your completed program should:
Each student completes one randomly selected action without partner help:
See checkoff_rubric.qmd for the criteria.
Submit three short responses: