logo

Question detail

Write a Python recursive function called countdown(n) that displays the values from n down to 1. State the base case and explain how the general case moves towards it.

Try the question, check the answer, then read the explanation to understand the curriculum point.

At a glance

Question

Type

practice

Style

Topic

Programming

Exam-style question

Try this first

Write a Python recursive function called countdown(n) that displays the values from n down to 1. State the base case and explain how the general case moves towards it.

Model answer

What a good answer should say

  • ```python def countdown(n): if n == 0: return print(n) countdown(n - 1) ``` The base case is `n == 0`, which stops the function without making another call.
  • The general case displays the current value and calls the function with `n - 1`, moving the argument towards zero.

Explanation

Why this works

A complete answer needs a stopping condition and a recursive call. The recursive call must change n so that repeated calls eventually reach the base case.

For example, countdown(3) displays 3, 2 and 1, then reaches countdown(0) and stops.

Common mistake

No common mistake is linked to this question yet.

Related flashcards

No flashcards are published for this page yet.

Related practice questions

No questions are published for this page yet.