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 oct_to_decimal() in conversions/octal_to_decimal.py when the input string (after optional '-' sign and strip) is not composed solely of octal digits 0-7. The function first checks isdigit(), then verifies every char is in 0..7; failing either raises this ValueError. It exists to reject decimal-looking inputs such as '19' that contain the digits 8 or 9.
Source
Thrown at conversions/octal_to_decimal.py:67
...
ValueError: Non-octal value was passed to the function
>>> oct_to_decimal("")
Traceback (most recent call last):
...
ValueError: Empty string was passed to the function
>>> oct_to_decimal("19")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
"""
oct_string = str(oct_string).strip()
if not oct_string:
raise ValueError("Empty string was passed to the function")
is_negative = oct_string[0] == "-"
if is_negative:
oct_string = oct_string[1:]
if not oct_string.isdigit() or not all(0 <= int(char) <= 7 for char in oct_string):
raise ValueError("Non-octal value was passed to the function")
decimal_number = 0
for char in oct_string:
decimal_number = 8 * decimal_number + int(char)
if is_negative:
decimal_number = -decimal_number
return decimal_number
if __name__ == "__main__":
from doctest import testmod
testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Sanitize the input before calling: keep only chars in '01234567' (plus optional leading '-') or reject early.
- If the value is decimal, convert it the other way (decimal_to_octal) or fix the upstream data producer.
- Strip a '0o'/'0O' prefix yourself before calling, since this function does not accept it.
- Wrap the call in try/except ValueError if the input is untrusted and show a validation message.
Example fix
# before
oct_to_decimal('19') # ValueError: Non-octal value was passed to the function
# after
valid = set('01234567')
s = '19'
if not s.lstrip('-').isdigit() or any(c not in valid for c in s.lstrip('-')):
raise ValueError(f'{s!r} is not an octal string')
oct_to_decimal(s) Defensive patterns
Strategy: validation
Validate before calling
def is_octal_string(s: str) -> bool:
s = str(s).strip().lstrip('-')
return s.isdigit() and all(0 <= int(c) <= 7 for c in s)
if not is_octal_string(user_value):
raise ValueError(f'{user_value!r} is not a valid octal string')
oct_to_decimal(user_value) Type guard
def is_octal_string(s: str) -> bool:
s = str(s).strip().lstrip('-')
return s.isdigit() and all(c in '01234567' for c in s) Try / catch
try:
oct_to_decimal(raw)
except ValueError as e:
if 'Non-octal' in str(e):
show_field_error('value must contain only digits 0-7')
else:
raise Prevention
- Reject or clean strings containing 8 or 9 before conversion
- Strip '0o' prefixes yourself; this function does not accept them
- Prefer int(s, 8) if you only need the number and can rely on Python's own parsing
When it happens
Trigger: Calling oct_to_decimal('19'), oct_to_decimal('8'), oct_to_decimal('abc') (fails isdigit), or any string containing '8'/'9' after a leading '-' is removed. Whitespace is stripped first, so ' 18 ' also triggers it.
Common situations: Feeding user input from a form or file directly into the converter; passing a decimal literal as a string because the source data was decimal, not octal; accidentally passing '0o17' (the 'o' fails isdigit).
Related errors
- Not a Valid Octal Number
- Expected a string as input
- Empty string was passed to the function
- Invalid 'from_type' value: {from_type!r} Supported values a
- Invalid 'to_type' value: {to_type!r}. Supported values are:
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/c4c826c1103b7906.
Report an issue: GitHub.