{"record":{"id":"f17cb4a71499f3c1","repo":"TheAlgorithms/Python","slug":"non-binary-value-was-passed-to-the-function","errorCode":null,"errorMessage":"Non-binary value was passed to the function","messagePattern":"Non-binary value was passed to the function","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/binary_to_decimal.py","lineNumber":33,"sourceCode":"        ...\n    ValueError: Non-binary value was passed to the function\n    >>> bin_to_decimal(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    >>> bin_to_decimal(\"39\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-binary value was passed to the function\n    \"\"\"\n    bin_string = str(bin_string).strip()\n    if not bin_string:\n        raise ValueError(\"Empty string was passed to the function\")\n    is_negative = bin_string[0] == \"-\"\n    if is_negative:\n        bin_string = bin_string[1:]\n    if not all(char in \"01\" for char in bin_string):\n        raise ValueError(\"Non-binary value was passed to the function\")\n    decimal_number = 0\n    for char in bin_string:\n        decimal_number = 2 * decimal_number + int(char)\n    return -decimal_number if is_negative else decimal_number\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":15,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/binary_to_decimal.py#L15-L44","documentation":"bin_to_decimal raises this ValueError when any character of the (possibly negative) input is not '0' or '1'. A leading '-' is allowed and stripped first, so '-101' is valid while '39', '10a1', or '-1-0' are rejected. The parser is character-by-character Horner-style, so it must reject non-binary digits before looping.","triggerScenarios":"bin_to_decimal('39'), bin_to_decimal('10O1') (letter O instead of zero), bin_to_decimal('0b101') — the '0b' prefix is not stripped and 'b' fails the check, bin_to_decimal('1 01') with an interior space.","commonSituations":"Passing Python repr output like bin(5) -> '0b101' directly; OCR/typo artifacts (O vs 0, l vs 1); strings containing underscores ('1_0' literal syntax) or spaces.","solutions":["Strip the 0b prefix before calling: s.removeprefix('0b')","Sanitize with a regex: re.fullmatch(r'-?[01]+', s)","For arbitrary-base strings, use int(s, 2) which handles 0b prefixes and whitespace itself"],"exampleFix":"# before\nbin_to_decimal(bin(5))  # '0b101'\n# ValueError: Non-binary value was passed to the function\n\n# after\nbin_to_decimal(bin(5).removeprefix('0b'))\n# or simply: int(bin(5), 2)","handlingStrategy":"type-guard","validationCode":"import re\nif not re.fullmatch(r'-?[01]+', bin_string.strip()):\n    raise ValueError(f'not a binary string: {bin_string!r}')","typeGuard":"def is_binary_string(s: str) -> bool:\n    s = s.strip().removeprefix('-')\n    return s != '' and all(c in '01' for c in s)","tryCatchPattern":"try:\n    bin_to_decimal(s)\nexcept ValueError as e:\n    if 'Non-binary' in str(e):\n        return int(s, 2) if re.fullmatch(r'-?[0-9]+', s or '') else None\n    raise","preventionTips":["Strip '0b' prefixes from bin() output before passing","Validate with regex at the input boundary","Consider int(s, 2) as the stdlib alternative"],"tags":["conversions","binary","validation","input-sanitization"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}