W WolfCode · CSC 141

Walkthrough: Recursive Countdown

例题: 递归倒计时

≈ 8 min · python-recursion · Open in WolfCode

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 caseif n <= 0: print("Blastoff!") — the "stop" condition.
  • Recursive casecountdown(n - 1) — call self with a smaller / closer-to-base argument.

Why <= 0, not == 0?

Don't actually run this — it will RecursionError