{"record":{"id":"c594a95ec390754f","repo":"TheAlgorithms/Python","slug":"non-binary-value-was-passed-to-the-function-c594a9","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_octal.py","lineNumber":23,"sourceCode":"'17'\n\n>>> bin_to_octal(\"101010101010011\")\n'52523'\n\n>>> bin_to_octal(\"\")\nTraceback (most recent call last):\n    ...\nValueError: Empty string was passed to the function\n>>> bin_to_octal(\"a-1\")\nTraceback (most recent call last):\n    ...\nValueError: Non-binary value was passed to the function\n\"\"\"\n\n\ndef bin_to_octal(bin_string: str) -> str:\n    if not all(char in \"01\" for char in bin_string):\n        raise ValueError(\"Non-binary value was passed to the function\")\n    if not bin_string:\n        raise ValueError(\"Empty string was passed to the function\")\n    oct_string = \"\"\n    while len(bin_string) % 3 != 0:\n        bin_string = \"0\" + bin_string\n    bin_string_in_3_list = [\n        bin_string[index : index + 3]\n        for index in range(len(bin_string))\n        if index % 3 == 0\n    ]\n    for bin_group in bin_string_in_3_list:\n        oct_val = 0\n        for index, val in enumerate(bin_group):\n            oct_val += int(2 ** (2 - index) * int(val))\n        oct_string += str(oct_val)\n    return oct_string\n\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/binary_to_octal.py#L5-L41","documentation":"bin_to_octal raises this ValueError when the input contains any character other than '0' or '1'. Note the check order: the non-binary scan runs first, so a string like 'a-1' or '2' hits this error, while a truly empty string passes all() (vacuously true) and is caught by the separate empty check below. No '-' sign is handled here — '-101' is rejected by this check.","triggerScenarios":"bin_to_octal('a-1'), bin_to_octal('2'), bin_to_octal('-101') (negative binary not supported, unlike bin_to_decimal), bin_to_octal('0b110') because of the 'b'.","commonSituations":"Reusing negative-binary handling from binary_to_decimal, which this function does not share; passing bin() repr with the 0b prefix; typo digits.","solutions":["Pass only raw '0'/'1' characters: strip prefixes and signs beforehand","Handle negatives yourself: sign = s.startswith('-'); bin_to_octal(s.lstrip('-')) with sign reapplied","For signed input, int(s, 2) then oct() is a simpler pipeline"],"exampleFix":"# before\nbin_to_octal('-101')\n# ValueError: Non-binary value was passed to the function\n\n# after\ns = '-101'\nsign = '-' if s.startswith('-') else ''\nsign + bin_to_octal(s.lstrip('-')).lstrip('0') or '0'","handlingStrategy":"type-guard","validationCode":"import re\nif not re.fullmatch(r'[01]+', bin_string):\n    raise ValueError(f'expected raw binary digits: {bin_string!r}')\nbin_to_octal(bin_string)","typeGuard":"def is_raw_binary(s) -> bool:\n    return isinstance(s, str) and s != '' and all(c in '01' for c in s)","tryCatchPattern":"try:\n    bin_to_octal(s)\nexcept ValueError as e:\n    if 'Non-binary' in str(e):\n        s = re.sub(r'[^01]', '', s)\n        return bin_to_octal(s)\n    raise","preventionTips":["This function has no '-' support — strip signs yourself","Strip '0b' prefixes before calling","Use int(s, 2) + oct() for tolerant pipelines"],"tags":["conversions","binary","octal","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}