{"record":{"id":"f337d2632939304a","repo":"TheAlgorithms/Python","slug":"str-object-cannot-be-interpreted-as-an-integer-f337d2","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_binary.py","lineNumber":33,"sourceCode":"    >>> # negatives work too\n    >>> decimal_to_binary_iterative(-2)\n    '-0b10'\n    >>> # other floats will error\n    >>> decimal_to_binary_iterative(16.16) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    TypeError: 'float' object cannot be interpreted as an integer\n    >>> # strings will error as well\n    >>> decimal_to_binary_iterative('0xfffff') # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    TypeError: 'str' object cannot be interpreted as an integer\n    \"\"\"\n\n    if isinstance(num, float):\n        raise TypeError(\"'float' object cannot be interpreted as an integer\")\n    if isinstance(num, str):\n        raise TypeError(\"'str' object cannot be interpreted as an integer\")\n\n    if num == 0:\n        return \"0b0\"\n\n    negative = False\n\n    if num < 0:\n        negative = True\n        num = -num\n\n    binary: list[int] = []\n    while num > 0:\n        binary.insert(0, num % 2)\n        num >>= 1\n\n    if negative:\n        return \"-0b\" + \"\".join(str(e) for e in binary)\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/decimal_to_binary.py#L15-L51","documentation":"Raised by decimal_to_binary_iterative(num) in conversions/decimal_to_binary.py:33 when `num` is a str. The function does no string parsing — not even numeric strings like '0xfffff' or '42' — because the shift/modulo loop requires an int. Callers must parse strings themselves before calling.","triggerScenarios":"decimal_to_binary_iterative('0xfffff'), decimal_to_binary_iterative('42'), or forwarding raw stdin/argparse/UI input into the function.","commonSituations":"Feeding CLI arguments (always strings) or file/network payloads directly to the converter; assuming the function is lenient like int() itself.","solutions":["Parse the string yourself first: decimal_to_binary_iterative(int('42')) for decimal, int('0xfffff', 0) or int(s, 16) for hex","For raw user input, wrap parsing in try/except ValueError to surface a friendly message","Note decimal_to_binary_recursive (same module) does accept strings — switch to it if string input is the norm"],"exampleFix":"# before\ndecimal_to_binary_iterative('0xfffff')  # TypeError\n\n# after\ndecimal_to_binary_iterative(int('0xfffff', 16))  # '0b11111111111111111111'","handlingStrategy":"type-guard","validationCode":"if isinstance(num, str):\n    num = int(num)          # decimal strings\n    # or int(num, 16) / int(num, 0) for prefixed strings","typeGuard":"def is_int_not_str(v: object) -> TypeGuard[int]:\n    return isinstance(v, int)","tryCatchPattern":null,"preventionTips":["Parse CLI/file input to int before any conversion call","For string inputs generally, prefer decimal_to_binary_recursive from the same module","Annotate parameters int and run mypy to catch string flows at build time"],"tags":["python","type-error","binary","string-parsing"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}