Lab, Functions
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 repeat(word, times=2, sep="") that returns word repeated times times with sep between the copies. Print repeat("go"), repeat("go", 3) and repeat("ha", 3, sep="-"), giving gogo, gogogo and ha-ha-ha.
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 each ___ so biggest accepts any number of numbers and label accepts any keyword options. The program prints 9, then colour=red and size=M.
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.
Task 05 .Fill the table with the variable values, step by step.
Each row shows a and b each time either changes, once both exist. Both are set from function results.
def power(base, exp=2):
return base ** exp
a = power(3)
b = power(2, 3)
a = power(exp=1, base=a)
b = power(b, exp=0)Sign 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: Pass nights and breakfast by keyword in the call.
The call to book passes three bare values that are hard to read. Rewrite the call so nights and breakfast are passed as keyword arguments. 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 invoice(customer, *amounts, tax=0.2, **notes) that returns a list of lines: Customer: NAME, then Subtotal: X, Total: Y (subtotal plus tax, both to 2 places), then one key: value line per note. Print each line of invoice("Cafe Rio", 12.5, 7.5, tax=0.1, due="Friday"):
Customer: Cafe Rio Subtotal: 20.00 Total: 22.00 due: Friday
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.