Walkthrough: Recursive Countdown
例题: 递归倒计时
The textbook's first recursive function. Same output as the Chapter 3 while-loop version, different shape.
Python · runnable
Every recursive function has two pieces
- Base case —
if n <= 0: print("Blastoff!")— the "stop" condition. - Recursive case —
countdown(n - 1)— call self with a smaller / closer-to-base argument.
Why <= 0, not == 0?
Don't actually run this — it will RecursionError