Lab, Text and Data
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. Watch when each read line appears.
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 validate(row) that returns a reason string or None. A row is a dict with name and age strings. Return "missing name" when the name is blank after stripping, "age is not a number" when the age is not all digits, "age out of range" when it is above 120, and None otherwise. The loop below must then print:
Ana None ? missing name Ben age is not a number Cy age out of range
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 clean becomes a generator stage that hands on each non-blank line in lower case, and the program prints ['apple', 'banana'].
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 counts the records and then totals them, but it prints total: 0. Fix it so it prints:
records: 3 total: 10
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 store, amount and totals each time one of them changes, once all three exist.
totals = {}
for store, amount in [("A", 5), ("B", 3), ("A", 2)]:
totals[store] = totals.get(store, 0) + amountSign 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: Make valid_amounts a generator: use yield, and no append.
This function builds a whole list before returning. Rewrite valid_amounts as a generator stage that yields each valid amount, with no list and no append. 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 pipeline(lines) that reads CSV lines with the header date,store,amount and returns (totals, rejects). totals maps each store name (stripped) to the sum of its amounts. A row with a blank store is rejected with "missing store"; an amount that is not a number, or is not above zero, with "bad amount". Each reject is a (line_number, reason) tuple, where the header is line 1. The program should print:
north: 19.75 south: 9.50 rejected: [(4, 'bad amount'), (5, 'missing store')]
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.