An engineering function has this Python 3 signature:
def pumping_power_kw(flow_l_s, pressure_kpa, efficiency): # function body goes here
Its contract requires nonnegative flow in litres per second, nonnegative pressure increase in kilopascals, and an efficiency satisfying 0<η≤10<\eta\leq10<η≤1. All inputs are finite real numbers. Any input outside this domain must raise ValueError; otherwise, the function must return input power in kilowatts.
Use
Which function body satisfies the entire contract, including its boundary cases?
if flow_l_s < 0 and pressure_kpa < 0 and not (0 < efficiency <= 1): raise ValueError("Inputs outside domain.") return flow_l_s * pressure_kpa / (1000.0 * efficiency)
if flow_l_s < 0 or pressure_kpa < 0 or not (0 < efficiency < 1): raise ValueError("Inputs outside domain.") return flow_l_s * pressure_kpa / (1000.0 * efficiency)
if flow_l_s < 0 or pressure_kpa < 0 or not (0 < efficiency <= 1): raise ValueError("Inputs outside domain.") return flow_l_s * pressure_kpa / efficiency
if flow_l_s < 0 or pressure_kpa < 0 or not (0 < efficiency <= 1): raise ValueError("Inputs outside domain.") return flow_l_s * pressure_kpa / (1000.0 * efficiency)
Unlock full course notes by signing up
Recognize a keyword-only interface
An engineer writes this Python function to calculate pressure from force in newtons and area in square metres:
Complete the statements about how its interface controls the following calls. Use the standard Python terminology and exact exception name.
In pressure(240.0, area_m2=0.006), the value for area_m2 is supplied as a ________. In contrast, pressure(240.0, 0.006) raises ________ before the function body executes.
Pass a calculation rule into a function
A numerical comparison tool uses the following Python functions. All inputs and outputs in this example are dimensionless.
Each table row represents a call to output_change with the listed function and numerical arguments. Complete the returned changes as exact integers. A function name without parentheses passes the function itself as an argument.
Separate local adjustment from caller state
A force sensor correction subtracts a supplied zero offset plus an additional 2.0 N calibration adjustment. Consider this Python 3 code:
After both calls finish, what are the values of first_n, second_n, and offset_n, respectively? All three values are in newtons.
Track a modified input and separate returned data
An engineer stores temperature readings in degrees Celsius and uses this Python function to add a reading and calculate deviations from the updated mean:
After every statement has executed, report average, samples[0], len(samples), and deviations[2]. Give the average, first sample, and last deviation in degrees Celsius to one decimal place. Give the number of samples as an exact integer. Python list indices start at zero.