Exercises
These exercises check the function habits: define, call, return, test, and keep inputs visible.
What line should be added to print hello?
Answer it first, then check.
Hint
The function name is greet. A call uses parentheses.
Solution
Add a function call:
The definition creates the function. The call runs it.
What value does this call return?
Compute it first, then check your number.
Hint
Only one argument is passed. Use the default value for bias.
Solution
The function is:
The call add_bias(7) uses the default bias=1, so it returns:
8
Fix the code so the output contains result: 10.
Return the value
Ready to run.
Hint
print() shows the value, but the caller receives None. Use return.
Solution
Use return:
Output:
result: 10
Fix the function so both assertions pass.
Squared error
Ready to run.
Hint
Squared error means:
(target - prediction) ** 2
Solution
Squared error subtracts and squares:
What value does this call return?
Compute it first, then check your number.
Hint
Substitute the arguments into:
weight * value + bias
Solution
Substitute into the return expression:
weight * value + bias
With weight=2, value=5, and bias=3:
2 * 5 + 3 = 13