TheAlgorithms/Python · error · TypeError

The input value of 'num_rows' should be 'int'

Error message

The input value of 'num_rows' should be 'int'

What it means

Raised by generate_pascal_triangle when num_rows is not an int (e.g. 7.89, "5"). The function builds rows by indexing and ranging, which require a true integer count. The isinstance check is strict: floats with integral values (5.0) and numpy integer scalars are also rejected, while bool passes because bool subclasses int.

Source

Thrown at matrix/pascal_triangle.py:61

    [[1], [1, 1]]
    >>> generate_pascal_triangle(3)
    [[1], [1, 1], [1, 2, 1]]
    >>> generate_pascal_triangle(4)
    [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]
    >>> 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]]

View on GitHub (pinned to f5988cc097)

Solutions

  1. Cast at the call site: generate_pascal_triangle(int(num_rows)) after range-validating.
  2. Use floor division (//) when deriving the count from arithmetic.
  3. Configure argparse with type=int so CLI input is already integral.

Example fix

# before
rows = generate_pascal_triangle(float(user_input))  # TypeError for 7.89

# after
rows = generate_pascal_triangle(int(float(user_input)))
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(num_rows, int) or isinstance(num_rows, bool):
    num_rows = int(float(num_rows))  # accept "7" / 7.0 from external input
result = generate_pascal_triangle(num_rows)

Type guard

def is_row_count(x) -> bool:
    """Guard: native int (bool excluded)."""
    return isinstance(x, int) and not isinstance(x, bool)

Try / catch

try:
    triangle = generate_pascal_triangle(n)
except TypeError as e:
    if "should be 'int'" in str(e):
        triangle = generate_pascal_triangle(int(float(n)))
    else:
        raise

Prevention

When it happens

Trigger: generate_pascal_triangle(7.89), generate_pascal_triangle(5.0), generate_pascal_triangle("5"), or a value computed with true division like n / 2. Values from argparse type=float also trigger it.

Common situations: Command-line or config input parsed as float/string; counts derived from len()/2-style divisions; JSON numbers decoded as floats.

Related errors


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