TheAlgorithms/Python · error · ValueError
This function only accepts non-negative integers
Error message
This function only accepts non-negative integers
What it means
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.
Source
Thrown at conversions/convert_number_to_words.py:122
>>> convert_small_number(10)
'ten'
>>> convert_small_number(15)
'fifteen'
>>> convert_small_number(20)
'twenty'
>>> convert_small_number(25)
'twenty-five'
>>> convert_small_number(-1)
Traceback (most recent call last):
...
ValueError: This function only accepts non-negative integers
>>> convert_small_number(123)
Traceback (most recent call last):
...
ValueError: This function only converts numbers less than 100
"""
if num < 0:
raise ValueError("This function only accepts non-negative integers")
if num >= 100:
raise ValueError("This function only converts numbers less than 100")
tens, ones = divmod(num, 10)
if tens == 0:
return NumberWords.ONES.value[ones] or "zero"
if tens == 1:
return NumberWords.TEENS.value[ones]
return (
NumberWords.TENS.value[tens]
+ ("-" if NumberWords.ONES.value[ones] else "")
+ NumberWords.ONES.value[ones]
)
def convert_number(
num: int, system: Literal["short", "long", "indian"] = "short"
) -> str:
"""View on GitHub (pinned to f5988cc097)
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))
Example fix
# before convert_small_number(-5) # ValueError: This function only accepts non-negative integers # after convert_number(-5) # 'negative five'
Defensive patterns
Strategy: validation
Validate before calling
if num < 0:
raise ValueError('use convert_number for negatives')
convert_small_number(num) Type guard
def is_small_non_negative(n) -> bool:
return isinstance(n, int) and 0 <= n < 100 Try / catch
try:
convert_small_number(n)
except ValueError:
return convert_number(n) # full converter handles any int Prevention
- 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
When it happens
Trigger: convert_small_number(-1); calling convert_small_number directly on parsed negative input instead of going through convert_number.
Common situations: Bypassing convert_number and reusing the internal helper; debt/temperature values passed to a helper designed for digit-group formatting.
Related errors
- This function only converts numbers less than 100
- Invalid numbering system
- Input number is too large
- Invalid 'from_type' value: {from_type!r}.\nConversion abbrev
- Invalid 'to_type' value: {to_type!r}.\nConversion abbreviati
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/7e61d6e558b564c9.
Report an issue: GitHub.