A4 Python for engineers · Practice
Making decisions with if
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
Write rating(P) that returns the smallest of 0.125, 0.25, 0.5, 1, 2 and 5 W that is at least twice P, or None if none is. Then set r = rating(V**2 / R) for V = 10 V across R = 15 Ω.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write status(measured, nominal, tol_pct) returning "low", "ok" or "high", where ok means within ±tol_pct percent of nominal (edges count as ok). Then set s = status(f * nom, nom, tol).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write diode_current(v, R): 0 for v up to 0.7 V, and above. Then set i = diode_current(v, R) for v = 0.3 V and R = 100 Ω.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write heater(on, T) with hysteresis: on below 19 °C, off above 21 °C, and unchanged in between. Starting with the heater off, set s1 = heater(False, T1) and then s2 = heater(s1, T2).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A meter has ranges of 0.2, 2, 20 and 200 V. Write pick_range(v) returning the smallest range whose full scale is at least the size of v (so −1.5 V needs the 2 V range), or the string "OL" if none is big enough. Then set rng = pick_range(v) for v = 0.05.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Without using max, set m to the largest of a = 8, b = 5 and c = 0.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write sign(x) returning −1, 0 or 1. Set s = sign(x) with x = 0.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set inside to whether V = 5 is between lo = 3 and hi = 13 inclusive, using a chained comparison.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4