Lab, Robust Code
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.
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 status_text(code) using match: 200 or 201 give ok; 404 gives not found; any int from 500 to 599 gives server error (use a guard); anything else gives unknown. Print the result for each code in [200, 201, 404, 503, 302], one per line.
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 a pattern that matches a two-item list and captures the items as x and y, so the program prints move to 3,4 and bad input.
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.
Only the key q should print bye; any other unknown key should print unknown. Right now x also prints bye. Fix the match so the program prints:
h help q bye x unknown
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 item and evens each time one of them changes, once both exist.
evens = 0
for item in [4, "x", 7, 10]:
match item:
case int(n) if n % 2 == 0:
evens = evens + 1
case _:
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: describe uses one match with case patterns: no elif and no isinstance.
This code works, but it checks each shape of message by hand. Rewrite describe with one match statement and case patterns, with no elif and no isinstance. 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.
Expressions are numbers or tuples: ("+", a, b), ("*", a, b), ("-", a) for negation, and ("max", a, b, ...) with any number of arguments. Write evaluate(expr) with match, calling itself on the parts, and raising ValueError(f"bad expression: {expr!r}") for anything else. The program should print:
7
-20
9
ValueError: bad expression: ('/', 1, 2)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.