TheAlgorithms/Python · error · TypeError
operation can not be conducted on an object of type {type(nu
Error message
operation can not be conducted on an object of type {type(number).__name__} What it means
Raised by get_reverse_bit_string() when its argument is not an int. The helper builds a 32-bit LSB-first bit string using % and >>=, operations the function deliberately refuses to run on non-integers. The message interpolates the actual type name (e.g. 'str', 'float') so callers can see what they passed.
Source
Thrown at bit_manipulation/reverse_bits.py:23
>>> get_reverse_bit_string(9)
'10010000000000000000000000000000'
>>> get_reverse_bit_string(43)
'11010100000000000000000000000000'
>>> get_reverse_bit_string(2873)
'10011100110100000000000000000000'
>>> get_reverse_bit_string(2550136832)
'00000000000000000000000000011001'
>>> get_reverse_bit_string("this is not a number")
Traceback (most recent call last):
...
TypeError: operation can not be conducted on an object of type str
"""
if not isinstance(number, int):
msg = (
"operation can not be conducted on an object of type "
f"{type(number).__name__}"
)
raise TypeError(msg)
bit_string = ""
for _ in range(32):
bit_string += str(number % 2)
number >>= 1
return bit_string
def reverse_bit(number: int) -> int:
"""
Take in a 32 bit integer, reverse its bits, return a 32 bit integer result
>>> reverse_bit(25)
2550136832
>>> reverse_bit(37)
2751463424
>>> reverse_bit(21)
2818572288
>>> reverse_bit(58)View on GitHub (pinned to f5988cc097)
Solutions
- Convert to int before calling: get_reverse_bit_string(int(value)).
- Validate at the boundary: if not isinstance(number, int): raise TypeError(...) in your own wrapper.
- If floats are legitimate, decide policy: int(x) truncates, round(x) rounds — then call.
Example fix
# before
get_reverse_bit_string("25") # TypeError: operation can not be conducted on an object of type str
# after
get_reverse_bit_string(int("25")) # ok Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(number, int):
number = int(number) # or raise your own error Type guard
def is_int(value: object) -> bool:
return isinstance(value, int) and not isinstance(value, bool) Try / catch
try:
get_reverse_bit_string(x)
except TypeError as e:
if 'operation can not be conducted' in str(e):
x = int(x) Prevention
- Keep numeric data as int end-to-end; avoid str/float round-trips.
- Centralize parsing (int()) at the I/O boundary so internals always see ints.
When it happens
Trigger: Calling get_reverse_bit_string('this is not a number'), get_reverse_bit_string(25.0), or passing any non-int object such as a list or None.
Common situations: Reading bits from JSON/text config where values arrive as strings, forwarding unvalidated CLI input, or passing a float that 'looks' integral (25.0).
Related errors
- Input value must be an 'int' type
- Input value must be a 'int' type
- the value of both inputs must be positive
- both inputs must be positive integers
- input must be a negative integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/6f2b07b4e137d26d.
Report an issue: GitHub.