Lab, Errors and Exceptions
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints. repr shows strings with quotes, which is how you spot text that only looks like a number.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write average(numbers), which returns the mean of a list, or 0 for an empty list instead of raising ZeroDivisionError. Print average([4, 6, 11]) and average([]).
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so a missing colour prints unknown instead of raising KeyError.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The program should print Total: 12.0 and Each: 4.0. It raises a TypeError because the price is text. Fix the cause, not just the print lines.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows d and results each time one of them changes, once both exist. The check skips the zero.
results = []
for d in [4, 0, 5]:
if d != 0:
results.append(20 // d)Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Use the in operator for the membership check: no range( and no indexing loop.
This code works, but it searches the list with an index loop. Rewrite the check with the in operator and no range( or index loop. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write problem(row), which returns a message for the first problem in a row dictionary, or None if it is fine: missing qty if the key is absent, qty not a number if it is not all digits, qty is zero if it is 0. Print each row's id with its problem, or ok. The output should be:
A ok B missing qty C qty not a number D qty is zero
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.