{"record":{"id":"f3af4716706687a3","repo":"TheAlgorithms/Python","slug":"invalid-input-num-must-be-0-and-sides-must-be","errorCode":null,"errorMessage":"Invalid input: num must be >= 0 and sides must be >= 3.","messagePattern":"Invalid input: num must be >= 0 and sides must be >= 3\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/special_numbers/polygonal_numbers.py","lineNumber":24,"sourceCode":"    >>> polygonal_num(0, 3)\n    0\n    >>> polygonal_num(3, 3)\n    6\n    >>> polygonal_num(5, 4)\n    25\n    >>> polygonal_num(2, 5)\n    5\n    >>> polygonal_num(-1, 0)\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid input: num must be >= 0 and sides must be >= 3.\n    >>> polygonal_num(0, 2)\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid input: num must be >= 0 and sides must be >= 3.\n    \"\"\"\n    if num < 0 or sides < 3:\n        raise ValueError(\"Invalid input: num must be >= 0 and sides must be >= 3.\")\n\n    return ((sides - 2) * num**2 - (sides - 4) * num) // 2\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":6,"sourceCodeEnd":33,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/special_numbers/polygonal_numbers.py#L6-L33","documentation":"Raised by polygonal_num(sides, num) in maths/special_numbers/polygonal_numbers.py when num < 0 or sides < 3. The closed-form formula ((sides-2)*num^2 - (sides-4)*num) // 2 needs a non-negative index and at least 3 sides (a polygon cannot have fewer), hence the combined guard with a single message.","triggerScenarios":"Calling polygonal_num(-1, 0) (negative num) or polygonal_num(0, 2) (sides < 3). Note the parameter order in the doctests: the first argument is the index (num), the second is sides — polygonal_num(2, 5) returns the 2nd pentagonal number, 5.","commonSituations":"Swapping the two positional arguments so a valid sides value lands in num's slot; looping sides from 2 (forgetting that digons are excluded); passing a negative index for 'previous element' semantics this API does not have.","solutions":["Double-check argument order: index first, sides second","Ensure sides >= 3 (triangles are the minimum) and num >= 0 in your loops and inputs","Use keyword arguments if unsure: polygonal_num(num=2, sides=5)"],"exampleFix":"// before\nval = polygonal_num(0, 2)  # ValueError: sides < 3\n\n// after\nval = polygonal_num(0, 3)  # first triangular number","handlingStrategy":"validation","validationCode":"def safe_polygonal(num: int, sides: int) -> int:\n    if num < 0:\n        raise ValueError('num (index) must be >= 0')\n    if sides < 3:\n        raise ValueError('sides must be >= 3 (triangle is the minimum)')\n    return polygonal_num(num, sides)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Argument order is (num, sides): index first","sides starts at 3, not 2","Use keyword args when unsure: polygonal_num(num=2, sides=5)"],"tags":["math","valueerror","input-validation","polygonal-numbers"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}