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. Follow which blocks run for each value.
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 to_number(text), which returns float(text), or None if the text is not a number. Use try and except ValueError. Print the results for "3.5", "-2" and "abc".
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 ___ with the exception that dividing by zero raises, so the program prints 5.0 and then no players.
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 0 when the stock file is missing. It catches the error but then raises a NameError. Fix the except block.
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.
The answers stand in for what a user typed. Each row shows answer and tries each time one of them changes, once both exist.
tries = 0
for answer in ["", "x", "7", "9"]:
tries = tries + 1
try:
age = int(answer)
break
except ValueError:
passSign 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: Catch KeyError by name; no bare except.
This code works, but its bare except: would hide any bug. Rewrite it to catch only KeyError. 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.
Each order line is item,qty. Write parse(line) that returns (item, qty) with qty as an int, and raises ValueError if the line has no comma or the quantity is not a whole number. Loop over the lines, collect good orders and print each bad line with skip:, then the total quantity. The output should be:
skip: 'bread' skip: 'jam,lots' Total: 5
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.