TheAlgorithms/Python · error · ValueError
Input value of [number={number}] must be > 0
Error message
Input value of [number={number}] must be > 0 What it means
Raised by catalan_number() when number is an int but < 1. The first Catalan number is C(1) = 1 (this implementation's convention — it returns 1 for number=1), so 0 and negatives have no value under its iterative product formula and are rejected. The type check has already passed by this point, so this error means 'right type, out-of-range value'.
Source
Thrown at maths/special_numbers/catalan_number.py:39
...
ValueError: Input value of [number=0] must be > 0
>>> catalan(-1)
Traceback (most recent call last):
...
ValueError: Input value of [number=-1] must be > 0
>>> catalan(5.0)
Traceback (most recent call last):
...
TypeError: Input value of [number=5.0] must be an integer
"""
if not isinstance(number, int):
msg = f"Input value of [number={number}] must be an integer"
raise TypeError(msg)
if number < 1:
msg = f"Input value of [number={number}] must be > 0"
raise ValueError(msg)
current_number = 1
for i in range(1, number):
current_number *= 4 * i - 2
current_number //= i + 1
return current_number
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Shift 0-based indices to the 1-based convention this function expects: catalan_number(i + 1)
- Guard the boundary: catalan_number(n) if n >= 1 else 1 (if your math needs C(0)=1)
- Start loops at 1: range(1, n + 1) instead of range(n)
Example fix
# before
for i in range(n):
values.append(catalan_number(i)) # i=0 -> ValueError
# after
for i in range(1, n + 1):
values.append(catalan_number(i)) Defensive patterns
Strategy: validation
Validate before calling
if number >= 1:
c = catalan_number(number)
else:
c = 1 # if your math assumes C(0) = 1, handle it yourself Type guard
def is_valid_catalan_index(value: object) -> bool:
return isinstance(value, int) and not isinstance(value, bool) and value >= 1 Prevention
- This API is 1-based; shift 0-based Catalan indices by +1
- Start loops at range(1, n + 1)
- catalan_number(1) == 1 is the smallest supported input
When it happens
Trigger: Calling catalan_number(0) or catalan_number(-1). Typical when an index computed as a difference or a parsed default lands on 0. Note that the standard math convention C(0) = 1 is NOT supported here — callers porting formulas that use 0-based Catalan indices must shift by one.
Common situations: Porting code that assumes C(0)=1 and calling with 0; loop bounds starting at 0 (for i in range(0, n): catalan_number(i)); user input of 0 treated as valid.
Related errors
- max_set_length must be non-negative
- Input list must be a non empty list
- Input series is not valid, valid series - [1, 2/3, 2]
- Input list must be a non empty list
- Input series cannot have 0 as an element
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/336927da202f73d3.
Report an issue: GitHub.