{"record":{"id":"a1697144cf00027d","repo":"TheAlgorithms/Python","slug":"empty-string-was-passed-to-the-function-a16971","errorCode":null,"errorMessage":"Empty string was passed to the function","messagePattern":"Empty string was passed to the function","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/binary_to_octal.py","lineNumber":25,"sourceCode":">>> 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\nif __name__ == \"__main__\":\n    from doctest import testmod","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/binary_to_octal.py#L7-L43","documentation":"bin_to_octal raises this ValueError when the input string is empty. The check is intentionally ordered after the non-binary scan (which empty strings vacuously pass), so blank input reaches this dedicated guard. It prevents the padding while-loop and 3-bit grouping from silently returning '0' for missing data.","triggerScenarios":"bin_to_octal(''), bin_to_octal('') from a split() empty token, bin_to_octal(None) would instead raise TypeError inside all(), so only genuinely empty strings land here.","commonSituations":"Blank user input; empty elements from splitting on consecutive delimiters; pipeline stages that legitimately produce empty strings and are not filtered.","solutions":["Filter empties before the call: [bin_to_octal(t) for t in tokens if t]","Substitute a default: bin_to_octal(s or '0')","Validate upstream form/CLI input with a required-field check"],"exampleFix":"# before\nfor token in '101,,11'.split(','):\n    bin_to_octal(token)\n# ValueError: Empty string was passed to the function\n\n# after\nfor token in '101,,11'.split(','):\n    if token:\n        bin_to_octal(token)","handlingStrategy":"validation","validationCode":"tokens = [t for t in raw.split(',') if t.strip()]\n[bin_to_octal(t) for t in tokens]","typeGuard":"def is_nonempty_str(v) -> bool:\n    return isinstance(v, str) and v != ''","tryCatchPattern":"try:\n    bin_to_octal(s)\nexcept ValueError as e:\n    if 'Empty string' in str(e):\n        return '0'\n    raise","preventionTips":["Skip empty split tokens","Substitute '0' for blank inputs when zero is intended","Add required-field checks upstream"],"tags":["conversions","binary","octal","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}