{"record":{"id":"7e61d6e558b564c9","repo":"TheAlgorithms/Python","slug":"this-function-only-accepts-non-negative-integers","errorCode":null,"errorMessage":"This function only accepts non-negative integers","messagePattern":"This function only accepts non-negative integers","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/convert_number_to_words.py","lineNumber":122,"sourceCode":"    >>> convert_small_number(10)\n    'ten'\n    >>> convert_small_number(15)\n    'fifteen'\n    >>> convert_small_number(20)\n    'twenty'\n    >>> convert_small_number(25)\n    'twenty-five'\n    >>> convert_small_number(-1)\n    Traceback (most recent call last):\n    ...\n    ValueError: This function only accepts non-negative integers\n    >>> convert_small_number(123)\n    Traceback (most recent call last):\n    ...\n    ValueError: This function only converts numbers less than 100\n    \"\"\"\n    if num < 0:\n        raise ValueError(\"This function only accepts non-negative integers\")\n    if num >= 100:\n        raise ValueError(\"This function only converts numbers less than 100\")\n    tens, ones = divmod(num, 10)\n    if tens == 0:\n        return NumberWords.ONES.value[ones] or \"zero\"\n    if tens == 1:\n        return NumberWords.TEENS.value[ones]\n    return (\n        NumberWords.TENS.value[tens]\n        + (\"-\" if NumberWords.ONES.value[ones] else \"\")\n        + NumberWords.ONES.value[ones]\n    )\n\n\ndef convert_number(\n    num: int, system: Literal[\"short\", \"long\", \"indian\"] = \"short\"\n) -> str:\n    \"\"\"","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/convert_number_to_words.py#L104-L140","documentation":"convert_small_number raises this ValueError when num is negative. The helper only formats numbers 0-99 using ONES/TEENS/TENS word tables, which have no negative entries; the top-level convert_number handles the sign itself by prepending 'negative', so the small-number helper never needs to.","triggerScenarios":"convert_small_number(-1); calling convert_small_number directly on parsed negative input instead of going through convert_number.","commonSituations":"Bypassing convert_number and reusing the internal helper; debt/temperature values passed to a helper designed for digit-group formatting.","solutions":["Call convert_number(num) instead — it handles negatives and large numbers end-to-end","If you must use the helper, strip the sign first and add the word yourself","Clamp inputs to 0-99 before calling: max(0, min(99, num))"],"exampleFix":"# before\nconvert_small_number(-5)\n# ValueError: This function only accepts non-negative integers\n\n# after\nconvert_number(-5)  # 'negative five'","handlingStrategy":"validation","validationCode":"if num < 0:\n    raise ValueError('use convert_number for negatives')\nconvert_small_number(num)","typeGuard":"def is_small_non_negative(n) -> bool:\n    return isinstance(n, int) and 0 <= n < 100","tryCatchPattern":"try:\n    convert_small_number(n)\nexcept ValueError:\n    return convert_number(n)  # full converter handles any int","preventionTips":["Prefer convert_number as the public entry point","Clamp or validate 0 <= n < 100 in tests of the helper","Keep helper usage internal to the module"],"tags":["conversions","numbers-to-words","validation","range-check"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}