TheAlgorithms/Python · error · TypeError
'str' object cannot be interpreted as an integer
Error message
'str' object cannot be interpreted as an integer
What it means
decimal_to_any raises TypeError('str' object cannot be interpreted as an integer') when the base argument is a str. The message matches CPython's int(x, '16') error on purpose. The base feeds divmod(num, base), which cannot work with strings; note this check runs after the num sign check, so a negative num with a str base raises the num error first.
Source
Thrown at conversions/decimal_to_any.py:65
...
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_value
div = num // base
num = divView on GitHub (pinned to f5988cc097)
Solutions
- Cast the base: decimal_to_any(34, int(base_str))
- Configure CLI parsing with type=int: argparse.add_argument('-b', type=int)
- Validate 2 <= int(base) <= 36 after casting (the function also checks range)
Example fix
# before
decimal_to_any(34, '16')
# TypeError: 'str' object cannot be interpreted as an integer
# after
decimal_to_any(34, int('16')) # '22' Defensive patterns
Strategy: type-guard
Validate before calling
base = int(base) # raises early if not numeric decimal_to_any(num, base)
Type guard
def is_valid_base(b) -> bool:
return isinstance(b, int) and not isinstance(b, bool) and 2 <= b <= 36 Try / catch
try:
decimal_to_any(num, base)
except TypeError as e:
if 'cannot be interpreted as an integer' in str(e):
return decimal_to_any(num, int(base))
raise Prevention
- Use argparse type=int for base arguments
- Cast config/JSON base values once at load
- Validate 2 <= base <= 36 right after casting
When it happens
Trigger: decimal_to_any(34, '16') — very common when CLI args or json values are not cast; decimal_to_any(34, '0x10'); passing a base typed by a user as text.
Common situations: argparse without type=int; JSON config where base was quoted; interactive input() left as a string; wrapping the function with **kwargs passed through from text sources.
Related errors
- int() can't convert non-string with explicit base
- parameter must be positive int
- operation can not be conducted on an object of type {type(nu
- Input value must be an 'int' type
- plaintext must be a string
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/4a5420dca60d8450.
Report an issue: GitHub.