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. Remember what Python does, and does not do, with annotations.
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(scores: list[float]) -> float, which returns the mean of the scores rounded to 1 decimal place, or 0.0 for an empty list. Print average([7.5, 9.0, 6.0]) 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 ___ with the return annotation saying the function gives a string or None, so the program prints str | None, ana@shop.com and None.
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 two quantities come from a form as text. total_items is annotated for ints, but nothing converts them, so the program prints 35. Fix the call so it prints 8.
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 price and total each time one of them changes, once both exist.
prices: list[float] = [2.5, 4.0, 1.5]
total: float = 0.0
for price in prices:
total = total + priceSign 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: label is fully annotated: name: str, count: int, unit: str = "item", -> str.
This code works, but label has no annotations. Annotate it: name is a str, count an int, unit a str with default "item", and it returns a str. 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 two fully annotated functions. parse(lines: list[str]) -> dict[str, list[int]] turns lines like "ana 7" into a dictionary of each name's marks, skipping lines that do not have a name and a whole number. top(book: dict[str, list[int]]) -> tuple[str, float] | None returns the name with the highest average and that average rounded to 1 place, or None for an empty book. The program should print:
{'ana': [7, 9], 'ben': [8], 'cai': [6, 10]}
('ana', 8.0)
NoneSign 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.