{"record":{"id":"c4c826c1103b7906","repo":"TheAlgorithms/Python","slug":"non-octal-value-was-passed-to-the-function-c4c826","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_decimal.py","lineNumber":67,"sourceCode":"        ...\n    ValueError: Non-octal value was passed to the function\n    >>> oct_to_decimal(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Empty string was passed to the function\n    >>> oct_to_decimal(\"19\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Non-octal value was passed to the function\n    \"\"\"\n    oct_string = str(oct_string).strip()\n    if not oct_string:\n        raise ValueError(\"Empty string was passed to the function\")\n    is_negative = oct_string[0] == \"-\"\n    if is_negative:\n        oct_string = oct_string[1:]\n    if not oct_string.isdigit() or not all(0 <= int(char) <= 7 for char in oct_string):\n        raise ValueError(\"Non-octal value was passed to the function\")\n    decimal_number = 0\n    for char in oct_string:\n        decimal_number = 8 * decimal_number + int(char)\n    if is_negative:\n        decimal_number = -decimal_number\n    return decimal_number\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":49,"sourceCodeEnd":80,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/octal_to_decimal.py#L49-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"# before\noct_to_decimal('19')  # ValueError: Non-octal value was passed to the function\n\n# after\nvalid = set('01234567')\ns = '19'\nif not s.lstrip('-').isdigit() or any(c not in valid for c in s.lstrip('-')):\n    raise ValueError(f'{s!r} is not an octal string')\noct_to_decimal(s)","handlingStrategy":"validation","validationCode":"def is_octal_string(s: str) -> bool:\n    s = str(s).strip().lstrip('-')\n    return s.isdigit() and all(0 <= int(c) <= 7 for c in s)\n\nif not is_octal_string(user_value):\n    raise ValueError(f'{user_value!r} is not a valid octal string')\noct_to_decimal(user_value)","typeGuard":"def is_octal_string(s: str) -> bool:\n    s = str(s).strip().lstrip('-')\n    return s.isdigit() and all(c in '01234567' for c in s)","tryCatchPattern":"try:\n    oct_to_decimal(raw)\nexcept ValueError as e:\n    if 'Non-octal' in str(e):\n        show_field_error('value must contain only digits 0-7')\n    else:\n        raise","preventionTips":["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"],"tags":["python","validation","numeral-system","conversion"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}