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 percent(part, whole) that returns part / whole * 100 rounded to 1 place. Use guard clauses: raise TypeError("numbers only") if either argument is not an int or float, and ValueError("whole must be positive") if whole is 0 or less. Print percent(3, 8), then try percent(3, 0) and percent("3", 8) and print each error's type name and message like ValueError: whole must be positive.
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 keyword that checks the condition and raises AssertionError with the message when it is false, so the program prints ok 6 and then AssertionError: counts do not match.
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.
top_two should return the two highest scores without changing the caller's list, so the program prints:
[91, 88] [72, 91, 65, 88]
Right now the caller's list comes back sorted. Fix top_two.
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 value and total each time one of them changes, once both exist.
total = 0
for value in [5, -2, 8, 0, 3]:
if value <= 0:
continue
total = total + valueSign 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: Guard clauses (if not ...: return) and no line indented more than 8 spaces.
This code works, but the checks are nested three levels deep. Rewrite can_book with guard clauses: one if not ...: per rule that returns its message straight away, and the success message at the end, with no line indented more than 8 spaces. 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 transfer(accounts, src, dst, amount) that moves money between two accounts in the accounts dictionary. Raise KeyError naming an unknown account, ValueError("amount must be positive"), ValueError("cannot transfer to the same account"), and ValueError(f"{src} has only {balance}") when the money is not there. Check everything before changing anything, then assert that the total of all balances is unchanged. The program should print:
ok {'ana': 70, 'ben': 50, 'cai': 0}
ValueError: ben has only 50
ValueError: amount must be positive
KeyError: 'zed'
ValueError: cannot transfer to the same account
total 120Sign 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.