TheAlgorithms/Python · error · TypeError

Input value must be a 'int' type

Error message

Input value must be a 'int' type

What it means

Raised by binary_count_trailing_zeros when the argument is a float. The function counts trailing zero bits via log2(a & -a), which only works on integers, so non-int inputs are rejected. Note the guard runs after `a < 0`, so a non-negative float hits this TypeError while a negative float first raises a TypeError from the `<` comparison against int.

Source

Thrown at bit_manipulation/binary_count_trailing_zeros.py:37

    >>> binary_count_trailing_zeros(0)
    0
    >>> binary_count_trailing_zeros(-10)
    Traceback (most recent call last):
        ...
    ValueError: Input value must be a positive integer
    >>> binary_count_trailing_zeros(0.8)
    Traceback (most recent call last):
        ...
    TypeError: Input value must be a 'int' type
    >>> binary_count_trailing_zeros("0")
    Traceback (most recent call last):
        ...
    TypeError: '<' not supported between instances of 'str' and 'int'
    """
    if a < 0:
        raise ValueError("Input value must be a positive integer")
    elif isinstance(a, float):
        raise TypeError("Input value must be a 'int' type")
    return 0 if (a == 0) else int(log2(a & -a))


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert to int before calling: binary_count_trailing_zeros(int(x)) when the value is a whole-number float.
  2. If fractional input is a bug, fix the caller to pass an integer count/index instead of a ratio or average.
  3. Wrap the call in a type check and raise a clearer domain-specific error upstream where the value originates.

Example fix

# before
binary_count_trailing_zeros(0.8)  # TypeError

# after
binary_count_trailing_zeros(int(0.8))  # 0
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(value, int) or isinstance(value, bool):
    raise TypeError(f"expected int, got {type(value).__name__}")

Type guard

def is_int(value: object) -> bool:
    return isinstance(value, int) and not isinstance(value, bool)

Prevention

When it happens

Trigger: Calling binary_count_trailing_zeros(0.8) or any non-negative float. Strings like "0" instead raise TypeError from `a < 0` before this line; negative floats raise ValueError('Input value must be a positive integer').

Common situations: Passing results of division, statistics functions, or JSON-parsed numbers that arrive as floats; forgetting to round/convert user or config input before a bit-manipulation call.

Related errors


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