TheAlgorithms/Python · error · ValueError

Non-octal value was passed to the function

Error message

Non-octal value was passed to the function

What it means

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').

Source

Thrown at conversions/octal_to_binary.py:39

        ...
    ValueError: Non-octal value was passed to the function
    >>> octal_to_binary("@#")
    Traceback (most recent call last):
        ...
    ValueError: Non-octal value was passed to the function
    >>> octal_to_binary("")
    Traceback (most recent call last):
        ...
    ValueError: Empty string was passed to the function
    """
    if not octal_number:
        raise ValueError("Empty string was passed to the function")

    binary_number = ""
    octal_digits = "01234567"
    for digit in octal_number:
        if digit not in octal_digits:
            raise ValueError("Non-octal value was passed to the function")

        binary_digit = ""
        value = int(digit)
        for _ in range(3):
            binary_digit = str(value % 2) + binary_digit
            value //= 2
        binary_number += binary_digit

    return binary_number


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Strip prefixes/signs/whitespace before calling: s = s.strip().removeprefix('0o'), handle '-' yourself
  2. Pre-validate with a regex: re.fullmatch(r'[0-7]+', s)
  3. 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'

Example fix

# before
octal_to_binary(oct(493))  # '0o755' -> ValueError

# after
octal_to_binary(oct(493).removeprefix('0o'))  # '111101101'
Defensive patterns

Strategy: validation

Validate before calling

import re
if not re.fullmatch(r'[0-7]+', octal_number.strip()):
    raise ValueError(f'{octal_number!r} is not an octal string')

Try / catch

try:
    octal_to_binary(octal_number)
except ValueError as e:
    if 'Non-octal' in str(e):
        cleaned = octal_number.strip().removeprefix('0o')
        if not set(cleaned) <= set('01234567'):
            raise
        print(octal_to_binary(cleaned))
    else:
        raise

Prevention

When it happens

Trigger: 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').

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/606346d5e1fc39aa. Report an issue: GitHub.