{"record":{"id":"606346d5e1fc39aa","repo":"TheAlgorithms/Python","slug":"non-octal-value-was-passed-to-the-function","errorCode":null,"errorMessage":"Non-octal value was passed to the function","messagePattern":"Non-octal value was passed to the function","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/octal_to_binary.py","lineNumber":39,"sourceCode":"        ...\n    ValueError: Non-octal value was passed to the function\n    >>> octal_to_binary(\"@#\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-octal value was passed to the function\n    >>> octal_to_binary(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    \"\"\"\n    if not octal_number:\n        raise ValueError(\"Empty string was passed to the function\")\n\n    binary_number = \"\"\n    octal_digits = \"01234567\"\n    for digit in octal_number:\n        if digit not in octal_digits:\n            raise ValueError(\"Non-octal value was passed to the function\")\n\n        binary_digit = \"\"\n        value = int(digit)\n        for _ in range(3):\n            binary_digit = str(value % 2) + binary_digit\n            value //= 2\n        binary_number += binary_digit\n\n    return binary_number\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":21,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/octal_to_binary.py#L21-L55","documentation":"Raised by octal_to_binary(octal_number) in conversions/octal_to_binary.py:39 when any character of the input is not one of the digits 0-7. Each octal digit must expand into a 3-bit group; digits 8/9, letters, signs, prefixes, or embedded whitespace all fail the per-character membership test and raise ValueError('Non-octal value was passed to the function').","triggerScenarios":"octal_to_binary('19'), octal_to_binary('0o17') ('o' rejected), octal_to_binary('755 '), octal_to_binary('-755') ('-' rejected — no sign support), octal_to_binary('8').","commonSituations":"Passing Python's oct() output ('0o755') without stripping the prefix; file-permission strings with extra characters; leading '+'/'-' signs; values that are actually decimal (e.g. '128') being treated as octal.","solutions":["Strip prefixes/signs/whitespace before calling: s = s.strip().removeprefix('0o'), handle '-' yourself","Pre-validate with a regex: re.fullmatch(r'[0-7]+', s)","If the input is an int, use format(n, 'o') style handling or convert via f'{n:o}' first rather than passing str(n) that may contain '8'/'9'"],"exampleFix":"# before\noctal_to_binary(oct(493))  # '0o755' -> ValueError\n\n# after\noctal_to_binary(oct(493).removeprefix('0o'))  # '111101101'","handlingStrategy":"validation","validationCode":"import re\nif not re.fullmatch(r'[0-7]+', octal_number.strip()):\n    raise ValueError(f'{octal_number!r} is not an octal string')","typeGuard":null,"tryCatchPattern":"try:\n    octal_to_binary(octal_number)\nexcept ValueError as e:\n    if 'Non-octal' in str(e):\n        cleaned = octal_number.strip().removeprefix('0o')\n        if not set(cleaned) <= set('01234567'):\n            raise\n        print(octal_to_binary(cleaned))\n    else:\n        raise","preventionTips":["Strip oct() '0o' prefixes before passing","Handle signs yourself — this function rejects '-' and '+'","Regex-validate [0-7]+ for any external octal input"],"tags":["python","value-error","octal","binary","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}