{"record":{"id":"aced6bc80503458e","repo":"TheAlgorithms/Python","slug":"length-must-be-a-positive-integer","errorCode":null,"errorMessage":"Length must be a positive integer.","messagePattern":"Length must be a positive integer\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/series/hexagonal_numbers.py","lineNumber":36,"sourceCode":"def hexagonal_numbers(length: int) -> list[int]:\r\n    \"\"\"\r\n    :param len: max number of elements\r\n    :type len: int\r\n    :return: Hexagonal numbers as a list\r\n\r\n    Tests:\r\n    >>> hexagonal_numbers(10)\r\n    [0, 1, 6, 15, 28, 45, 66, 91, 120, 153]\r\n    >>> hexagonal_numbers(5)\r\n    [0, 1, 6, 15, 28]\r\n    >>> hexagonal_numbers(0)\r\n    Traceback (most recent call last):\r\n      ...\r\n    ValueError: Length must be a positive integer.\r\n    \"\"\"\r\n\r\n    if length <= 0 or not isinstance(length, int):\r\n        raise ValueError(\"Length must be a positive integer.\")\r\n    return [n * (2 * n - 1) for n in range(length)]\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    print(hexagonal_numbers(length=5))\r\n    print(hexagonal_numbers(length=10))\r\n","sourceCodeStart":18,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/series/hexagonal_numbers.py#L18-L43","documentation":"Raised by hexagonal_numbers() in maths/series/hexagonal_numbers.py when length is not an int or is <= 0. The function generates the first `length` hexagonal numbers via a list comprehension over range(length), so a negative, zero, or non-integer length is meaningless. Note the check order: `length <= 0 or not isinstance(length, int)` — a float like 5.0 is also rejected even though it equals a valid int.","triggerScenarios":"Calling hexagonal_numbers(0), hexagonal_numbers(-3), or hexagonal_numbers(5.0). Booleans pass (isinstance(True, int) is True) but length=False raises because False <= 0.","commonSituations":"User-supplied count parsed from a string without int() conversion; a computed length that went negative after subtraction; division results (e.g. n/2 in Python 3 yields float) passed directly.","solutions":["Coerce to int when the value is whole: hexagonal_numbers(int(n))","Clamp or validate the computed length: length = max(1, int(length))","Reject non-integer input at the UI/API boundary with a clear message"],"exampleFix":"# before\nhexagonal_numbers(n / 2)  # float -> ValueError\n\n# after\nhexagonal_numbers(int(n / 2))  # or n // 2","handlingStrategy":"validation","validationCode":"length = int(length) if isinstance(length, float) and length.is_integer() else length\nif isinstance(length, int) and length > 0:\n    print(hexagonal_numbers(length))","typeGuard":"def is_positive_int(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value > 0","tryCatchPattern":null,"preventionTips":["Use // instead of / for computed counts","Parse CLI/JSON counts with int()","Clamp computed lengths: max(1, int(n))"],"tags":["python","math","validation","value-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}