TheAlgorithms/Python · error · TypeError
only integers accepted as input
Error message
only integers accepted as input
What it means
remove_digit() in maths/remove_digit.py computes the largest number obtainable by deleting one digit from num. It requires isinstance(num, int) and raises TypeError('only integers accepted as input') otherwise, because it does str(abs(num)) and builds digit-transposition lists — a non-int either breaks str/abs or produces character slices that int() cannot reparse. This function correctly uses TypeError (unlike several siblings that use ValueError).
Source
Thrown at maths/remove_digit.py:25
>>> remove_digit(152)
52
>>> remove_digit(6385)
685
>>> remove_digit(-11)
1
>>> remove_digit(2222222)
222222
>>> remove_digit("2222222")
Traceback (most recent call last):
TypeError: only integers accepted as input
>>> remove_digit("string input")
Traceback (most recent call last):
TypeError: only integers accepted as input
"""
if not isinstance(num, int):
raise TypeError("only integers accepted as input")
else:
num_str = str(abs(num))
num_transpositions = [list(num_str) for char in range(len(num_str))]
for index in range(len(num_str)):
num_transpositions[index].pop(index)
return max(
int("".join(list(transposition))) for transposition in num_transpositions
)
if __name__ == "__main__":
__import__("doctest").testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Convert the value first: remove_digit(int(num)) after confirming it is numeric.
- Parse and validate strings at the edge (int(text) in try/except ValueError) and pass the int.
- Catch TypeError (this function's contract) rather than ValueError.
Example fix
# before remove_digit(form_value) # TypeError when form_value is '2222222' # after remove_digit(int(form_value))
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(num, int):
num = int(num)
remove_digit(num) Type guard
def is_strict_int(v) -> bool:
return isinstance(v, int) and not isinstance(v, bool) Try / catch
try:
remove_digit(num)
except TypeError:
num = int(num) # this function raises TypeError, unlike siblings Prevention
- int() every form/request value before math helpers.
- This module uses TypeError; the persistence/prime modules use ValueError — check each contract.
- Keep parsing at the boundary, math inside.
When it happens
Trigger: Calling remove_digit('2222222') or remove_digit('string input'); any call forwarding input() text, a web-parameter string, or a float like 2222222.0 without conversion.
Common situations: Passing request/form values (always strings) directly; JSON data where the number arrived as a string; assuming the function parses text because its doctests show digit strings in error cases.
Related errors
- perfect_cube_binary_search() only accepts integers
- 'float' object cannot be interpreted as an integer
- 'float' object cannot be interpreted as an integer
- iterations must be defined as integers
- All weights must be integers but got weight of type {type(wt
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/055b61233c10f795.
Report an issue: GitHub.