countdown(n) that recursively builds and returns a list of integers counting down from n to 0 (inclusive), e.g. countdown(3) -> [3, 2, 1, 0]. Add explicit guard clauses so it never runs away: raise ValueError immediately (without recursing) if n is negative, and also raise ValueError if n > 900 since Python's default recursion limit is around 1000 and this would otherwise crash with an unhelpful RecursionError.countdown(3)
[3, 2, 1, 0]
Counts down one step per recursive call.
countdown(-1)
ValueError raised
Guard clause rejects negative input before recursing.
Put both guard checks at the very top of the function, before the base/recursive cases.
Base case for the countdown itself: n == 0 returns [0].
Recursive case: [n] + countdown(n - 1).