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. Each exception is caught by the first except that matches it.
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 check_percent(n), which returns n when it is between 0 and 100 inclusive, and otherwise raises ValueError with the message percent must be 0 to 100, got N. Print check_percent(42), then try check_percent(150) and print the error message.
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 QuotaError is an exception class and the program prints Stopped: 3 uploads is the limit.
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.
A card decline should print try another card and other payment problems payment failed. Right now a decline prints the general message. Fix the program so it prints:
try another card payment failed
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 ok and rejected each time one of them changes, once both exist.
def check(n):
if n < 0:
raise ValueError("negative")
return n
ok = 0
rejected = 0
for n in [3, -1, 4, -5]:
try:
check(n)
ok = ok + 1
except ValueError:
rejected = rejected + 1Sign 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: find_user raises KeyError for an unknown id, never returns None, and the caller uses try and except.
This code works, but find_user returns None for an unknown id and the caller must remember to check. Rewrite find_user to raise KeyError with a message, and the caller to use try and except. 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.
Define BookingError, and two subclasses SoldOutError and TooManyError. Write book(event, qty), which raises TooManyError if qty is more than 4, SoldOutError if the event has fewer seats left than qty, and BookingError (from the KeyError) for an unknown event; otherwise it takes the seats and returns how many are left. Process the requests so the output is:
gig: 1 left TooManyError: max 4 per order SoldOutError: play has 0 left BookingError: no event 'opera'
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.