{"record":{"id":"870c8863b7120c43","repo":"TheAlgorithms/Python","slug":"input-must-be-a-positive-integer-870c88","errorCode":null,"errorMessage":"Input must be a positive integer","messagePattern":"Input must be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/special_numbers/hexagonal_number.py","lineNumber":42,"sourceCode":"    946\n    >>> hexagonal(0)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input must be a positive integer\n    >>> hexagonal(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input must be a positive integer\n    >>> hexagonal(11.0)\n    Traceback (most recent call last):\n        ...\n    TypeError: Input value of [number=11.0] must be an integer\n    \"\"\"\n    if not isinstance(number, int):\n        msg = f\"Input value of [number={number}] must be an integer\"\n        raise TypeError(msg)\n    if number < 1:\n        raise ValueError(\"Input must be a positive integer\")\n    return number * (2 * number - 1)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":24,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/special_numbers/hexagonal_number.py#L24-L50","documentation":"Raised by hexagonal(number) when the argument is a builtin int but is less than 1. The hexagonal sequence 1, 6, 15, 28, ... is indexed from 1, so there is no 0th or negative hexagonal number and the guard rejects them after the isinstance check passes.","triggerScenarios":"Calling hexagonal(0), hexagonal(-1), or any negative/zero integer. Non-integers raise the TypeError instead because the type check runs first.","commonSituations":"Off-by-one loop bounds (range(0, n) instead of range(1, n+1)); passing a 0-based index from another sequence API into this 1-based API without shifting.","solutions":["Start loops at 1: `for n in range(1, count + 1)`","If you have a 0-based index, add 1 before calling: hexagonal(idx + 1)","Validate that user-supplied positions are >= 1 before invoking"],"exampleFix":"// before\nh = hexagonal(0)  # ValueError\n\n// after\nh = hexagonal(0 + 1)  # first hexagonal number","handlingStrategy":"validation","validationCode":"if not isinstance(n, int) or n < 1:\n    raise ValueError('hexagonal index must be a positive int')\nh = hexagonal(n)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["This API is 1-based: first hexagonal number is hexagonal(1)","Use range(1, n + 1), not range(0, n)","Shift 0-based indices by +1 before calling"],"tags":["math","valueerror","input-validation","off-by-one"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}