Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1if n == 0: return 1 | Adef forever(n): return forever(n - 1) | |
| 2return n * factorial(n - 1) | Bdef countdown(n): if n == 0: return countdown(n - 1) | |
| 3A countdown function stops at its base case and otherwise calls itself with a smaller number | CThe recursive call makes the problem bigger (n + 1), so n never reaches the base case. It should be countdown(n - 1). | |
| 4A function with no base case never stops calling itself, so Python raises a RecursionError | DThere is no base case, so the function never stops and raises a RecursionError. Add if n == 0: return 1 first. | |
| 5def factorial(n): return n * factorial(n - 1) | EThe base case: it returns a value without calling the function again, so the recursion stops | |
| 6def countdown(n): if n == 0: return countdown(n + 1) | FThe recursive case: the function calls itself with a smaller problem (n - 1) |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
recursion — A function that calls itself with a smaller problem each time, and stops at a base case that returns without calling itself. With no base case, Python raises a RecursionError.def factorial(n):
if n == 0:
return 1def fact(n):
if n <= 1: return 1
return n * fact(n - 1)def total(n):
if n == 0: return 0
return n + total(n - 1)def forever(n):
return forever(n + 1)
# Raises RecursionErrordef countdown(n):
if n == 0: return
countdown(n - 1)