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
- Convert to int before calling: binary_count_trailing_zeros(int(x)) when the value is a whole-number float.
- If fractional input is a bug, fix the caller to pass an integer count/index instead of a ratio or average.
- 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
- Convert division/average results with int() before any bit-manipulation call.
- Keep numeric data as int end-to-end; avoid round-tripping through float.
- Run mypy or pyright in strict mode so float leaks into int parameters are caught statically.
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
- Both arguments MUST be integers!
- Input must be a non-negative integer
- Input must be a non-negative integer
- all elements must be integers
- Input value must be an 'int' type
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/6a35cdd37110e603.
Report an issue: GitHub.