{"record":{"id":"a055f5f6690e1cfd","repo":"TheAlgorithms/Python","slug":"the-input-value-of-num-rows-should-be-int","errorCode":null,"errorMessage":"The input value of 'num_rows' should be 'int'","messagePattern":"The input value of 'num_rows' should be 'int'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"matrix/pascal_triangle.py","lineNumber":61,"sourceCode":"    [[1], [1, 1]]\n    >>> generate_pascal_triangle(3)\n    [[1], [1, 1], [1, 2, 1]]\n    >>> generate_pascal_triangle(4)\n    [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]\n    >>> generate_pascal_triangle(5)\n    [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]\n    >>> generate_pascal_triangle(-5)\n    Traceback (most recent call last):\n        ...\n    ValueError: The input value of 'num_rows' should be greater than or equal to 0\n    >>> generate_pascal_triangle(7.89)\n    Traceback (most recent call last):\n        ...\n    TypeError: The input value of 'num_rows' should be 'int'\n    \"\"\"\n\n    if not isinstance(num_rows, int):\n        raise TypeError(\"The input value of 'num_rows' should be 'int'\")\n\n    if num_rows == 0:\n        return []\n    elif num_rows < 0:\n        raise ValueError(\n            \"The input value of 'num_rows' should be greater than or equal to 0\"\n        )\n\n    triangle: list[list[int]] = []\n    for current_row_idx in range(num_rows):\n        current_row = populate_current_row(triangle, current_row_idx)\n        triangle.append(current_row)\n    return triangle\n\n\ndef populate_current_row(triangle: list[list[int]], current_row_idx: int) -> list[int]:\n    \"\"\"\n    >>> triangle = [[1]]","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/matrix/pascal_triangle.py#L43-L79","documentation":"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.","triggerScenarios":"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.","commonSituations":"Command-line or config input parsed as float/string; counts derived from len()/2-style divisions; JSON numbers decoded as floats.","solutions":["Cast at the call site: generate_pascal_triangle(int(num_rows)) after range-validating.","Use floor division (//) when deriving the count from arithmetic.","Configure argparse with type=int so CLI input is already integral."],"exampleFix":"# before\nrows = generate_pascal_triangle(float(user_input))  # TypeError for 7.89\n\n# after\nrows = generate_pascal_triangle(int(float(user_input)))","handlingStrategy":"type-guard","validationCode":"if not isinstance(num_rows, int) or isinstance(num_rows, bool):\n    num_rows = int(float(num_rows))  # accept \"7\" / 7.0 from external input\nresult = generate_pascal_triangle(num_rows)","typeGuard":"def is_row_count(x) -> bool:\n    \"\"\"Guard: native int (bool excluded).\"\"\"\n    return isinstance(x, int) and not isinstance(x, bool)","tryCatchPattern":"try:\n    triangle = generate_pascal_triangle(n)\nexcept TypeError as e:\n    if \"should be 'int'\" in str(e):\n        triangle = generate_pascal_triangle(int(float(n)))\n    else:\n        raise","preventionTips":["Use argparse type=int for CLI row counts.","Use // rather than / when computing counts.","Cast once at the input boundary so both pascal variants receive a validated int."],"tags":["matrix","pascal-triangle","typeerror","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}