Exercises

These exercises check the function habits: define, call, return, test, and keep inputs visible.

Exercise: Call the function

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.

Exercise: Returned value

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
Exercise: Return instead of print

Fix the code so the output contains result: 10.

Return the value

Runs locally with Python in your browser.

Ready to run.

Hint

print() shows the value, but the caller receives None. Use return.

Solution

Use return:

Output:

result: 10
Exercise: Test squared error

Fix the function so both assertions pass.

Squared error

Runs locally with Python in your browser.

Ready to run.

Hint

Squared error means:

(target - prediction) ** 2
Solution

Squared error subtracts and squares:

Exercise: Weighted score

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