TheAlgorithms/Python · error · ValueError
parameter must be positive int
Error message
parameter must be positive int
What it means
decimal_to_any raises ValueError('parameter must be positive int') when num < 0. The repeated-division algorithm (divmod until the quotient reaches 1) assumes a positive dividend; negatives would loop incorrectly. Note the message says 'positive' but 0 is accepted, and the float TypeError check runs before this, so only negative ints land here.
Source
Thrown at conversions/decimal_to_any.py:63
>>> decimal_to_any(10, '16') # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as an integer
>>> # a base less than 2 will error
>>> decimal_to_any(7, 0) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: base must be >= 2
>>> # a base greater than 36 will error
>>> decimal_to_any(34, 37) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: base must be <= 36
"""
if isinstance(num, float):
raise TypeError("int() can't convert non-string with explicit base")
if num < 0:
raise ValueError("parameter must be positive int")
if isinstance(base, str):
raise TypeError("'str' object cannot be interpreted as an integer")
if isinstance(base, float):
raise TypeError("'float' object cannot be interpreted as an integer")
if base in (0, 1):
raise ValueError("base must be >= 2")
if base > 36:
raise ValueError("base must be <= 36")
new_value = ""
mod = 0
div = 0
while div != 1:
div, mod = divmod(num, base)
if base >= 11 and 9 < mod < 36:
actual_value = ALPHABET_VALUES[str(mod)]
else:
actual_value = str(mod)
new_value += actual_valueView on GitHub (pinned to f5988cc097)
Solutions
- Handle the sign yourself: convert abs(num) and prefix '-' (or two's-complement for fixed width)
- Clamp at the source if negatives are invalid in your domain: max(0, num)
- Catch this ValueError alongside the TypeError/ValueError base checks when user input flows through
Example fix
# before decimal_to_any(-10, 2) # ValueError: parameter must be positive int # after sign = '-' if n < 0 else '' sign + decimal_to_any(abs(-10), 2) # '-1010'
Defensive patterns
Strategy: validation
Validate before calling
if num < 0:
sign, num = '-', -num
else:
sign = ''
result = sign + decimal_to_any(num, base) Type guard
def is_non_negative_int(v) -> bool:
return isinstance(v, int) and not isinstance(v, bool) and v >= 0 Try / catch
try:
decimal_to_any(num, base)
except ValueError as e:
if 'positive int' in str(e):
return '-' + decimal_to_any(-num, base)
raise Prevention
- Move sign handling to the caller
- Clamp deltas with max(0, x) when negatives are impossible
- Remember 0 is accepted; only negatives are rejected
When it happens
Trigger: decimal_to_any(-7, 2), decimal_to_any(-255, 16); computing deltas/offsets that can go negative and passing them unclamped.
Common situations: Temperature/difference conversions; arithmetic on unsigned-vs-signed mixes; tests that sweep negative values through every conversion helper.
Related errors
- Invalid 'from_type' value: {from_type!r}.\nConversion abbrev
- Invalid 'to_type' value: {to_type!r}.\nConversion abbreviati
- Empty string was passed to the function
- Non-binary value was passed to the function
- Non-binary value was passed to the function
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/ea970cd9bd702e8b.
Report an issue: GitHub.