TheAlgorithms/Python · error · TypeError

int() can't convert non-string with explicit base

Error message

int() can't convert non-string with explicit base

What it means

decimal_to_any raises TypeError('int() can\'t convert non-string with explicit base') when num is a float. The message deliberately mirrors CPython's own error for int(float, base) because this function is a drop-in base-conversion routine: positional-notation conversion is only defined for integers. Fractional parts would be silently truncated otherwise.

Source

Thrown at conversions/decimal_to_any.py:61

    TypeError: 'float' object cannot be interpreted as an integer
    >>> # a str base will error
    >>> 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:

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert to int deliberately first: decimal_to_any(int(num), base) if truncation is intended
  2. Reject or round fractional values explicitly: int(round(num)) when appropriate
  3. Use isinstance(num, float) checks at your own API boundary to give a better message

Example fix

# before
decimal_to_any(7.0, 2)
# TypeError: int() can't convert non-string with explicit base

# after
decimal_to_any(int(7.0), 2)  # '111'
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(num, float):
    if not num.is_integer():
        raise ValueError('fractional values unsupported')
    num = int(num)
decimal_to_any(num, base)

Type guard

def is_int_like(v) -> bool:
    return isinstance(v, int) and not isinstance(v, bool)

Try / catch

try:
    decimal_to_any(num, base)
except TypeError as e:
    if 'non-string with explicit base' in str(e):
        return decimal_to_any(int(num), base)
    raise

Prevention

When it happens

Trigger: decimal_to_any(7.0, 2), decimal_to_any(3.14, 16), receiving float-typed values from JSON parsing or numpy scalars (np.float64) without casting.

Common situations: Data pipelines where numbers arrive as floats (json, pandas, numpy defaults); user input converted with float() instead of int(); division results passed without rounding.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/274bfd33f7972fb2. Report an issue: GitHub.