EE1021 Programming for Engineers · Lecture 3 Lab · 2026–2027
By the end of the lab, you should be able to:
math modulesensor_tools.py and call them from main.pyFor every task:
Keep functions focused: one function should have one clear job.
result Become None?Before running:
10.result is not 10.The function displays 10, then returns None. Replace print(value * 2) with return value * 2 when the caller needs the result.
Open starter/return_vs_print_bug.py.
print() in the main code.5, 0, and -3.Expected results from the repaired function:
| Argument | Returned value |
|---|---|
| 5 | 10 |
| 0 | 0 |
| -3 | -6 |
Create a function with this contract:
def calibrate_voltage(raw_voltage, offset=0.05):
"""Return raw_voltage corrected by offset."""
# your codeTest these calls:
print(calibrate_voltage(4.92))
print(calibrate_voltage(4.92, 0.10))
print(calibrate_voltage(0.0, -0.02))Record the three expected values before running the program.
Answer in a comment:
raw_voltage not have a default?Do not create separate functions for standard and custom offsets. One parameterized function should handle both cases.
Implement:
def voltage_status(voltage):
"""Return Invalid, Low, Normal, or High."""
# Invalid: voltage < 0
# Low: 0 <= voltage < 4.75
# Normal: 4.75 <= voltage <= 5.25
# High: voltage > 5.25Your function must return exactly one string and must not call input().
Use a separate call for every test.
print(voltage_status(-0.1))
print(voltage_status(4.74))
print(voltage_status(4.75))
print(voltage_status(5.25))
print(voltage_status(5.26))Create a short test table in a comment:
| Input | Expected | Actual |
|---|
Correct the function if any expected and actual values differ.
Implement:
def sum_multiples(limit, divisor):
"""Return the sum of positive multiples up to limit."""
total = 0
# add a for loop
return totalRequired tests:
Explain why the last call returns 0.
Show the instructor or teaching assistant:
calibrate_voltage() with a default argumentvoltage_status() passing all five boundary testssum_multiples() passing all three testsinput() belongs in the main code rather than these calculation functionsDo not continue until your functions return the expected values.
math ModuleFor a series circuit, calculate impedance magnitude:
|Z| = square root of (resistance² + reactance²)
import math
def impedance_magnitude(resistance, reactance):
# return the magnitude using math.sqrt
passTest:
sensor_tools.py
main.py
sensor_toolsDownload or open sensor_tools.py and main.py. Keep them in the same folder.
sensor_tools.pydef calibrate_voltage(raw_voltage, offset=0.05):
"""Return raw_voltage corrected by offset."""
pass
def voltage_status(voltage):
"""Return Invalid, Low, Normal, or High."""
passReplace every pass. Keep the function names and parameters unchanged so that main.py can call them.
main.py
import sensor_tools
raw_voltage = 4.72
calibrated = sensor_tools.calibrate_voltage(raw_voltage)
status = sensor_tools.voltage_status(calibrated)
print("Raw:", raw_voltage)
print("Calibrated:", calibrated)
print("Status:", status)Run main.py, then test at least three raw readings that produce different statuses.
Demonstrate a working two-file program and explain:
sensor_tools.?calibrate_voltage() return?sensor_tools.py were in a different folder?Submit or retain the completed files according to the instructor’s directions.
The intended expression is:
√2 × sin(π/4)
Explain why the two expressions differ and verify that correct is approximately 1.0.