TheAlgorithms/Python · error · ValueError
Empty string was passed to the function
Error message
Empty string was passed to the function
What it means
bin_to_decimal raises this ValueError when the input, after str() conversion and .strip(), is empty. The guard runs before any parsing, so an empty or all-whitespace string never reaches the digit loop. It exists to distinguish 'no input at all' from 'input that is not binary' (a separate error).
Source
Thrown at conversions/binary_to_decimal.py:28
-29
>>> bin_to_decimal("0")
0
>>> bin_to_decimal("a")
Traceback (most recent call last):
...
ValueError: Non-binary value was passed to the function
>>> bin_to_decimal("")
Traceback (most recent call last):
...
ValueError: Empty string was passed to the function
>>> bin_to_decimal("39")
Traceback (most recent call last):
...
ValueError: Non-binary value was passed to the function
"""
bin_string = str(bin_string).strip()
if not bin_string:
raise ValueError("Empty string was passed to the function")
is_negative = bin_string[0] == "-"
if is_negative:
bin_string = bin_string[1:]
if not all(char in "01" for char in bin_string):
raise ValueError("Non-binary value was passed to the function")
decimal_number = 0
for char in bin_string:
decimal_number = 2 * decimal_number + int(char)
return -decimal_number if is_negative else decimal_number
if __name__ == "__main__":
from doctest import testmod
testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Check the string is non-empty before calling: if not s.strip(): ...
- Filter blank lines when processing files: (line for line in lines if line.strip())
- Default missing input to '0' if an empty string should mean zero in your domain
Example fix
# before
bin_to_decimal(user_input) # user pressed Enter
# ValueError: Empty string was passed to the function
# after
if not user_input.strip():
raise ValueError('binary string required')
bin_to_decimal(user_input) Defensive patterns
Strategy: validation
Validate before calling
if not str(bin_string).strip():
raise ValueError('binary string required')
bin_to_decimal(bin_string) Type guard
def is_nonempty_str(v) -> bool:
return isinstance(v, str) and v.strip() != '' Try / catch
try:
bin_to_decimal(s)
except ValueError as e:
if 'Empty string' in str(e):
return 0 # or skip record
raise Prevention
- Filter blank lines/tokens before batch conversion
- Make binary fields required in input validation
- Default empty form fields to '0' explicitly
When it happens
Trigger: bin_to_decimal(''), bin_to_decimal(' '), bin_to_decimal(None) (str(None)='None' actually fails the binary check instead), bin_to_decimal(' \t ') — any input that is empty after trimming whitespace.
Common situations: Reading user input that was submitted blank; iterating over file lines where a trailing empty line is not filtered; variable initialized to '' and never set before the call.
Related errors
- Non-binary value was passed to the function
- Empty string was passed to the function
- Non-binary value was passed to the function
- Non-binary value was passed to the function
- Empty string was passed to the function
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/b63431987649216a.
Report an issue: GitHub.