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 1

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp negative counts to 0: generate_pascal_triangle(max(0, n)) if an empty triangle is acceptable.
  2. Validate and reject user input early: if n < 0 raise/re-prompt at the input layer.
  3. 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

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


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/c1039a51ef7986b1. Report an issue: GitHub.