Exercises
These exercises ask you to read errors as evidence and make small fixes.
This code fails:
What is the error type?
Select one choice, then check.
Hint
The list is empty. The function tries to read one item by index.
Solution
The error type is:
IndexError
items is an empty list, so items[0] asks for a first item that does not
exist.
Edit the code so it prints 12.
Use the defined name
Ready to run.
Hint
Compare the spelling on the assignment line with the spelling inside print.
Solution
One fix is:
The original code assigned total but printed totel. Python treats those as
different names.
Edit the code so it prints 10.
Convert the text score
Ready to run.
Hint
score starts as text. Arithmetic needs a number.
Solution
One fix is:
The conversion changes the text "8" into the integer 8, so addition with
2 is numeric.
Which assertion checks that values is not empty before computing a mean?
Answer it first, then check.
Hint
The mean divides by the length. Division by zero is the assumption to prevent.
Solution
Use:
assert len(values) > 0
A mean divides by the number of values. The assertion states that the divisor must not be zero.
This program fails:
Which smallest example preserves the same kind of failure?
Select one choice, then check.
Hint
The dictionary and loop are not required to show the failure. Keep the addition between an integer and a string.
Solution
A reduced failing example is:
This keeps the same kind of failure: adding an integer and a string. It removes the list, dictionary, and loop so the broken assumption is easier to see.