{"record":{"id":"274bfd33f7972fb2","repo":"TheAlgorithms/Python","slug":"int-can-t-convert-non-string-with-explicit-base","errorCode":null,"errorMessage":"int() can't convert non-string with explicit base","messagePattern":"int\\(\\) can't convert non-string with explicit base","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"conversions/decimal_to_any.py","lineNumber":61,"sourceCode":"    TypeError: 'float' object cannot be interpreted as an integer\n    >>> # a str base will error\n    >>> decimal_to_any(10, '16') # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    TypeError: 'str' object cannot be interpreted as an integer\n    >>> # a base less than 2 will error\n    >>> decimal_to_any(7, 0) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: base must be >= 2\n    >>> # a base greater than 36 will error\n    >>> decimal_to_any(34, 37) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: base must be <= 36\n    \"\"\"\n    if isinstance(num, float):\n        raise TypeError(\"int() can't convert non-string with explicit base\")\n    if num < 0:\n        raise ValueError(\"parameter must be positive int\")\n    if isinstance(base, str):\n        raise TypeError(\"'str' object cannot be interpreted as an integer\")\n    if isinstance(base, float):\n        raise TypeError(\"'float' object cannot be interpreted as an integer\")\n    if base in (0, 1):\n        raise ValueError(\"base must be >= 2\")\n    if base > 36:\n        raise ValueError(\"base must be <= 36\")\n    new_value = \"\"\n    mod = 0\n    div = 0\n    while div != 1:\n        div, mod = divmod(num, base)\n        if base >= 11 and 9 < mod < 36:\n            actual_value = ALPHABET_VALUES[str(mod)]\n        else:","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/decimal_to_any.py#L43-L79","documentation":"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.","triggerScenarios":"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.","commonSituations":"Data pipelines where numbers arrive as floats (json, pandas, numpy defaults); user input converted with float() instead of int(); division results passed without rounding.","solutions":["Convert to int deliberately first: decimal_to_any(int(num), base) if truncation is intended","Reject or round fractional values explicitly: int(round(num)) when appropriate","Use isinstance(num, float) checks at your own API boundary to give a better message"],"exampleFix":"# before\ndecimal_to_any(7.0, 2)\n# TypeError: int() can't convert non-string with explicit base\n\n# after\ndecimal_to_any(int(7.0), 2)  # '111'","handlingStrategy":"type-guard","validationCode":"if isinstance(num, float):\n    if not num.is_integer():\n        raise ValueError('fractional values unsupported')\n    num = int(num)\ndecimal_to_any(num, base)","typeGuard":"def is_int_like(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    decimal_to_any(num, base)\nexcept TypeError as e:\n    if 'non-string with explicit base' in str(e):\n        return decimal_to_any(int(num), base)\n    raise","preventionTips":["Cast JSON/pandas/numpy numerics to int at the boundary","Parse user input with int() not float() for base conversions","Handle bool separately: isinstance(True, int) is True"],"tags":["conversions","type-error","float","base-conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}