TheAlgorithms/Python · error · ValueError
Empty string was passed to the function
Error message
Empty string was passed to the function
What it means
Raised by oct_to_decimal(oct_string) in conversions/octal_to_decimal.py:62 when, after str()/strip(), the value is empty. Like its hex sibling, the function first rejects blank input with ValueError('Empty string was passed to the function') before looking at digits or the optional leading '-'.
Source
Thrown at conversions/octal_to_decimal.py:62
0
>>> oct_to_decimal("-4055")
-2093
>>> oct_to_decimal("2-0Fm")
Traceback (most recent call last):
...
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
- Treat blank as missing before calling: if not str(value).strip(): handle the missing case
- Default to '0' when zero is the sensible interpretation: oct_to_decimal(value.strip() or '0')
- Use argparse defaults/validation so empty strings never reach the converter
Example fix
# before
oct_to_decimal(args.octal) # unset optional flag '' -> ValueError
# after
val = args.octal.strip() if args.octal else ''
if not val:
raise SystemExit('octal value required')
oct_to_decimal(val) Defensive patterns
Strategy: validation
Validate before calling
oct_string = str(oct_string).strip()
if not oct_string:
raise ValueError('octal value is required') Type guard
def is_nonempty_str(v: object) -> TypeGuard[str]:
return isinstance(v, str) and bool(v.strip()) Prevention
- Reject blank optional inputs at the CLI/config layer
- Default to '0' when zero is the intended empty semantics
- str()-normalize inputs early since the function itself calls str()
When it happens
Trigger: oct_to_decimal(''), oct_to_decimal(' '), oct_to_decimal(None) (str(None)='None' actually fails the digit check instead — only truly empty/blank strings hit this branch), or empty fields from argparse/file parsing.
Common situations: Optional CLI flags or config keys that default to '' when unset; iterating over split() results that include empty strings; form fields left blank.
Related errors
- Empty string was passed to the function
- Empty string was passed to the function
- No input value was provided
- No value was passed to the function
- Non-hexadecimal value was passed to the function
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/7ca703bcec9cd44c.
Report an issue: GitHub.