TheAlgorithms/Python · error · TypeError
Input value of [number={number}] must be an integer
Error message
Input value of [number={number}] must be an integer What it means
Raised by hexagonal(number) in maths/special_numbers/hexagonal_number.py when the argument is not an int. It computes the n-th hexagonal number with the closed formula n*(2n-1); because isinstance(number, int) is strict, floats (even integral ones like 11.0), strings, and None all fail. Note that bool passes since bool subclasses int.
Source
Thrown at maths/special_numbers/hexagonal_number.py:40
231
>>> hexagonal(22)
946
>>> hexagonal(0)
Traceback (most recent call last):
...
ValueError: Input must be a positive integer
>>> hexagonal(-1)
Traceback (most recent call last):
...
ValueError: Input must be a positive integer
>>> hexagonal(11.0)
Traceback (most recent call last):
...
TypeError: Input value of [number=11.0] must be an integer
"""
if not isinstance(number, int):
msg = f"Input value of [number={number}] must be an integer"
raise TypeError(msg)
if number < 1:
raise ValueError("Input must be a positive integer")
return number * (2 * number - 1)
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Convert integral floats before calling: hexagonal(int(x)) when float(x).is_integer()
- Fix upstream data types (e.g. use dtype int columns or json int parsing) so the value is a builtin int
- Add your own isinstance check at the API boundary with a clearer domain-specific message
Example fix
// before h = hexagonal(11.0) # TypeError // after n = int(11.0) h = hexagonal(n)
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(n, int) or isinstance(n, bool):
n = int(n) # only after confirming integrality
h = hexagonal(n) Type guard
def is_builtin_int(v) -> TypeGuard[int]:
return isinstance(v, int) and not isinstance(v, bool) Try / catch
try:
h = hexagonal(n)
except TypeError:
n = int(n)
h = hexagonal(n) Prevention
- Normalize float->int at the JSON/pandas boundary
- Watch out: bool passes the isinstance(int) check
- Use integer division // to keep indices integral
When it happens
Trigger: Calling hexagonal(11.0), hexagonal('5'), or hexagonal(None). Floats are rejected even when they hold an exact integer value; use of numpy integer types may also fail depending on version.
Common situations: Data arriving from JSON or pandas as floats (11.0 instead of 11), or from a division result; mixing numpy int64 scalars into a codebase that expects builtin ints.
Related errors
- Input value of [number={number}] must be an integer
- Input value of [number={number}] must be an integer
- Input value of [{number=}] must be an integer
- Input value of [number={number}] must be an integer
- Useful years must be an integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/fbbcb74e7918d6d0.
Report an issue: GitHub.