{"record":{"id":"4a5420dca60d8450","repo":"TheAlgorithms/Python","slug":"str-object-cannot-be-interpreted-as-an-integer","errorCode":null,"errorMessage":"'str' object cannot be interpreted as an integer","messagePattern":"'str' object cannot be interpreted as an integer","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"conversions/decimal_to_any.py","lineNumber":65,"sourceCode":"        ...\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:\n            actual_value = str(mod)\n        new_value += actual_value\n        div = num // base\n        num = div","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/decimal_to_any.py#L47-L83","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"# before\ndecimal_to_any(34, '16')\n# TypeError: 'str' object cannot be interpreted as an integer\n\n# after\ndecimal_to_any(34, int('16'))  # '22'","handlingStrategy":"type-guard","validationCode":"base = int(base)  # raises early if not numeric\ndecimal_to_any(num, base)","typeGuard":"def is_valid_base(b) -> bool:\n    return isinstance(b, int) and not isinstance(b, bool) and 2 <= b <= 36","tryCatchPattern":"try:\n    decimal_to_any(num, base)\nexcept TypeError as e:\n    if 'cannot be interpreted as an integer' in str(e):\n        return decimal_to_any(num, int(base))\n    raise","preventionTips":["Use argparse type=int for base arguments","Cast config/JSON base values once at load","Validate 2 <= base <= 36 right after casting"],"tags":["conversions","type-error","base-conversion","string-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}