TheAlgorithms/Python · error · ValueError

Input value is not an integer

Error message

Input value is not an integer

What it means

Raised by decimal_to_binary_recursive(number) in conversions/decimal_to_binary.py:100 when the (sign-stripped) input fails str.isnumeric() — i.e. it is not composed purely of digit characters. After str(number).strip() and lstrip('-'), anything left over that is not numeric (letters, '40.8' with a dot, hex strings) is rejected with a ValueError before recursion starts.

Source

Thrown at conversions/decimal_to_binary.py:100

    '0b101000'
    >>> decimal_to_binary_recursive(-40)
    '-0b101000'
    >>> decimal_to_binary_recursive(40.8)
    Traceback (most recent call last):
        ...
    ValueError: Input value is not an integer
    >>> decimal_to_binary_recursive("forty")
    Traceback (most recent call last):
        ...
    ValueError: Input value is not an integer
    """
    number = str(number).strip()
    if not number:
        raise ValueError("No input value was provided")
    negative = "-" if number.startswith("-") else ""
    number = number.lstrip("-")
    if not number.isnumeric():
        raise ValueError("Input value is not an integer")
    return f"{negative}0b{decimal_to_binary_recursive_helper(int(number))}"


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    print(decimal_to_binary_recursive(input("Input a decimal number: ")))

View on GitHub (pinned to f5988cc097)

Solutions

  1. For floats, convert to int explicitly first: decimal_to_binary_recursive(int(40.8)) (truncates) or round()
  2. Restrict input to integer strings: strip whitespace, verify with s.lstrip('-').isnumeric() before calling
  3. For hex/binary source strings, parse with int(s, 16)/int(s, 2) first, then pass the int

Example fix

# before
decimal_to_binary_recursive(40.8)  # ValueError

# after
decimal_to_binary_recursive(int(40.8))  # '0b101000'
Defensive patterns

Strategy: validation

Validate before calling

s = str(number).strip().lstrip('-')
if not s.isnumeric():
    raise ValueError(f'{number!r} is not an integer')
# floats: use int(number) first if fractional part should be truncated

Try / catch

try:
    decimal_to_binary_recursive(number)
except ValueError:
    number = int(float(number))  # last-resort coercion for numeric strings/floats
    result = decimal_to_binary_recursive(number)

Prevention

When it happens

Trigger: decimal_to_binary_recursive('forty'), decimal_to_binary_recursive(40.8) (str(40.8) = '40.8' contains '.'), decimal_to_binary_recursive('0x1F'), or inputs with spaces, commas, plus signs, or underscores.

Common situations: Passing floats directly — a frequent surprise since 40.8 stringifies to '40.8' and the dot fails isnumeric(); accepting free-text numeric input; localized numbers using ',' decimal separators.

Related errors


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