{"record":{"id":"c1039a51ef7986b1","repo":"TheAlgorithms/Python","slug":"the-input-value-of-num-rows-should-be-greater-th","errorCode":null,"errorMessage":"The input value of 'num_rows' should be greater than or equal to 0","messagePattern":"The input value of 'num_rows' should be greater than or equal to 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"matrix/pascal_triangle.py","lineNumber":66,"sourceCode":"    >>> 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]]\n    >>> populate_current_row(triangle, 1)\n    [1, 1]\n    \"\"\"\n    current_row = [-1] * (current_row_idx + 1)\n    # first and last elements of current row are equal to 1","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/matrix/pascal_triangle.py#L48-L84","documentation":"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.","triggerScenarios":"generate_pascal_triangle(-5), or a computed count like len(data) - offset that went negative when data was smaller than expected.","commonSituations":"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'.","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)."],"exampleFix":"# before\nrows = generate_pascal_triangle(len(data) - 10)  # negative when len < 10\n\n# after\nrows = generate_pascal_triangle(max(0, len(data) - 10))","handlingStrategy":"validation","validationCode":"if num_rows < 0:\n    raise ValueError(f\"num_rows must be >= 0, got {num_rows}\")\nresult = generate_pascal_triangle(num_rows)","typeGuard":"def is_non_negative_int(x) -> bool:\n    \"\"\"Guard: int, not bool, and >= 0.\"\"\"\n    return isinstance(x, int) and not isinstance(x, bool) and x >= 0","tryCatchPattern":"try:\n    triangle = generate_pascal_triangle(n)\nexcept ValueError as e:\n    if \"greater than or equal to 0\" in str(e):\n        triangle = generate_pascal_triangle(max(0, n))  # empty triangle for bad input\n    else:\n        raise","preventionTips":["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."],"tags":["matrix","pascal-triangle","range-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}