TheAlgorithms/Python · error · ValueError

No value was passed to the function

Error message

No value was passed to the function

What it means

Raised by hex_to_bin(hex_num) in conversions/hex_to_bin.py:34-38 when the input strips to an empty string. The function expects a hexadecimal string; an empty or whitespace-only string carries no digits to convert, so it raises ValueError('No value was passed to the function') before attempting int(hex_num, 16).

Source

Thrown at conversions/hex_to_bin.py:34

    >>> hex_to_bin("   12f   ")
    100101111
    >>> hex_to_bin("FfFf")
    1111111111111111
    >>> hex_to_bin("-fFfF")
    -1111111111111111
    >>> hex_to_bin("F-f")
    Traceback (most recent call last):
        ...
    ValueError: Invalid value was passed to the function
    >>> hex_to_bin("")
    Traceback (most recent call last):
        ...
    ValueError: No value was passed to the function
    """

    hex_num = hex_num.strip()
    if not hex_num:
        raise ValueError("No value was passed to the function")

    is_negative = hex_num[0] == "-"
    if is_negative:
        hex_num = hex_num[1:]

    try:
        int_num = int(hex_num, 16)
    except ValueError:
        raise ValueError("Invalid value was passed to the function")

    bin_str = ""
    while int_num > 0:
        bin_str = str(int_num % 2) + bin_str
        int_num >>= 1

    return int(("-" + bin_str) if is_negative else bin_str)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Guard for blank input before calling: if not hex_str.strip(): skip/handle
  2. Supply a sensible default ('0') when the field is optional
  3. Validate required CLI args early (argparse required=True) so empty values never reach the converter

Example fix

# before
hex_to_bin(hex_arg)  # empty argv -> ValueError

# after
if not hex_arg.strip():
    raise SystemExit('hex value required')
print(hex_to_bin(hex_arg))
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(hex_num, str) or not hex_num.strip():
    raise ValueError('a hexadecimal string is required')

Type guard

def is_nonempty_str(v: object) -> TypeGuard[str]:
    return isinstance(v, str) and bool(v.strip())

Prevention

When it happens

Trigger: hex_to_bin(''), hex_to_bin(' '), hex_to_bin('-') after sign stripping in caller code, or passing an empty variable read from a file/arg/env.

Common situations: Programmatic pipelines where a previous step produced an empty hex field; CLI scripts consuming argv without checking presence; optional config keys defaulting to ''.

Related errors


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