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 a recursive function power(base, exp) for a whole number exp of 0 or more: power(base, 0) is 1, and otherwise it is base * power(base, exp - 1). Do not use **. Print power(2, 5) and power(7, 0), giving 32 and 1.
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 reverse reverses a string recursively and the program prints nohtyp.
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.
sum_to(n) should return 1 + 2 + ... + n, so the program prints 15. It has two bugs: the recursive call does not get smaller, and its result is not returned. Fix them.
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 call has its own items and rest. A row is recorded in whichever call is running, each time those two change, once both exist in that call. Fill in the rows as the calls return.
def total(items):
if not items:
return 0
rest = total(items[1:])
return items[0] + rest
answer = total([4, 5, 6])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 the score in and return the new score instead of using global.
This program changes a global with the global keyword. Rewrite add_points to take the current score as a parameter and return the new score, with no global. The output must stay Score: 15.
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 a recursive function flatten(items) that returns a single list of all the numbers in a list that may contain other lists at any depth. Use isinstance(item, list) to spot an inner list. Print flatten([1, [2, 3], [4, [5, [6]]]]) and the sum of the result:
[1, 2, 3, 4, 5, 6] 21
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.