Recursive Sum to n
Python
15 pts
Recursive Sum to n
Write a recursive function sum_to(n) that returns the sum of all whole numbers from 1 to n. For example sum_to(4) is 4 + 3 + 2 + 1 = 10.
- Base case:
sum_to(0)returns0. - Recursive case:
sum_to(n)isn + sum_to(n - 1).
Do not use a loop or sum() — the function must call itself. Print sum_to(5) and sum_to(10).
Expected output:
15 55
Checklist
- Define
sum_to(n)with a base case:sum_to(0)returns0. - Add the recursive case so that the function calls itself with a smaller number (no loop, no
sum()). - Print
sum_to(5)andsum_to(10).
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
