TheAlgorithms/Python · error · ValueError
The input value of 'num_rows' should be greater than or equa
Error message
The input value of 'num_rows' should be greater than or equal to 0
What it means
Raised by generate_pascal_triangle when num_rows is a negative integer. A triangle with a negative number of rows is meaningless, so the function validates the range after the type check and before building rows. Note that 0 is valid and returns an empty list; only strictly negative values raise.
Source
Thrown at matrix/pascal_triangle.py:66
>>> generate_pascal_triangle(5)
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
>>> generate_pascal_triangle(-5)
Traceback (most recent call last):
...
ValueError: The input value of 'num_rows' should be greater than or equal to 0
>>> generate_pascal_triangle(7.89)
Traceback (most recent call last):
...
TypeError: The input value of 'num_rows' should be 'int'
"""
if not isinstance(num_rows, int):
raise TypeError("The input value of 'num_rows' should be 'int'")
if num_rows == 0:
return []
elif num_rows < 0:
raise ValueError(
"The input value of 'num_rows' should be greater than or equal to 0"
)
triangle: list[list[int]] = []
for current_row_idx in range(num_rows):
current_row = populate_current_row(triangle, current_row_idx)
triangle.append(current_row)
return triangle
def populate_current_row(triangle: list[list[int]], current_row_idx: int) -> list[int]:
"""
>>> triangle = [[1]]
>>> populate_current_row(triangle, 1)
[1, 1]
"""
current_row = [-1] * (current_row_idx + 1)
# first and last elements of current row are equal to 1View on GitHub (pinned to f5988cc097)
Solutions
- Clamp negative counts to 0: generate_pascal_triangle(max(0, n)) if an empty triangle is acceptable.
- Validate and reject user input early: if n < 0 raise/re-prompt at the input layer.
- Debug the arithmetic that produced the negative value (e.g. offset larger than length).
Example fix
# before rows = generate_pascal_triangle(len(data) - 10) # negative when len < 10 # after rows = generate_pascal_triangle(max(0, len(data) - 10))
Defensive patterns
Strategy: validation
Validate before calling
if num_rows < 0:
raise ValueError(f"num_rows must be >= 0, got {num_rows}")
result = generate_pascal_triangle(num_rows) Type guard
def is_non_negative_int(x) -> bool:
"""Guard: int, not bool, and >= 0."""
return isinstance(x, int) and not isinstance(x, bool) and x >= 0 Try / catch
try:
triangle = generate_pascal_triangle(n)
except ValueError as e:
if "greater than or equal to 0" in str(e):
triangle = generate_pascal_triangle(max(0, n)) # empty triangle for bad input
else:
raise Prevention
- Clamp derived counts with max(0, n) when an empty triangle is acceptable.
- Reject negative sizes at the input layer with a domain-specific message.
- Treat -1 sentinels from other APIs as 'unset' and convert them before passing here.
When it happens
Trigger: generate_pascal_triangle(-5), or a computed count like len(data) - offset that went negative when data was smaller than expected.
Common situations: Offsets/subtractions producing negative counts on small inputs; user-supplied sizes not range-checked at the boundary; sentinel values like -1 passed from another API meaning 'unlimited'.
Related errors
- power is negative
- The number of columns in the first matrix must be equal to t
- Only square matrices can be raised to a power
- Only invertable matrices can be raised to a negative power
- Step size must be positive and non-zero.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/c1039a51ef7986b1.
Report an issue: GitHub.