A4 Python for engineers · Practice
Errors, exceptions and testing
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 current(V, R) that returns V / R, but raises a ValueError if R is zero or negative. Then set I = current(V, R) for V = 5 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 safe_float(text) that returns float(text), or None if the text isn't a number. Then set vals to safe_float applied to each string in the starter's list.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The starter's mean_abs has a bug. Fix it so it returns the mean of the absolute values, then set m = mean_abs(xs).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Look up each name in the parts dictionary. Set values to the list of their values, using 0 for any name that isn't in it, without letting a KeyError stop the program.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write to_int(text) returning int(text), or −1 if that fails. Set n = to_int(s) for the starter's string.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write rc_tau(R, C) returning R * C, raising ValueError if either is negative. Set t = rc_tau(1000, tau / 1000).
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