TheAlgorithms/Python · error · TypeError
k must be an integer.
Error message
k must be an integer.
What it means
Raised by the recursive fibonacci helper in searches/fibonacci_search.py when k is not an int. fibonacci_search uses Fibonacci numbers to pick probe offsets, and list indexing with a non-integer offset would be meaningless, so the helper hard-rejects non-int inputs with TypeError. Because isinstance(k, int) is the test, floats like 5.0 are rejected even though they are integer-valued, and bool passes (True/False are int subclasses).
Source
Thrown at searches/fibonacci_search.py:47
Fibonacci number in position k.
>>> fibonacci(0)
0
>>> fibonacci(2)
1
>>> fibonacci(5)
5
>>> fibonacci(15)
610
>>> fibonacci('a')
Traceback (most recent call last):
TypeError: k must be an integer.
>>> fibonacci(-5)
Traceback (most recent call last):
ValueError: k integer must be greater or equal to zero.
"""
if not isinstance(k, int):
raise TypeError("k must be an integer.")
if k < 0:
raise ValueError("k integer must be greater or equal to zero.")
if k == 0:
return 0
elif k == 1:
return 1
else:
return fibonacci(k - 1) + fibonacci(k - 2)
def fibonacci_search(arr: list, val: int) -> int:
"""A pure Python implementation of a fibonacci search algorithm.
Parameters
----------
arr
List of sorted elements.
valView on GitHub (pinned to f5988cc097)
Solutions
- Pass a plain Python int: fibonacci(10).
- Coerce at the boundary: fibonacci(int(k)) after confirming no data loss (e.g. k == int(k)).
- For values loaded from JSON, convert with int() before calling the helper.
Example fix
# before fibonacci(float(user_input)) # e.g. 15.0 -> TypeError # after fibonacci(int(float(user_input))) # or int(user_input) for plain digit strings
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(k, int) or isinstance(k, bool):
k = int(k) # or raise TypeError
fibonacci(k) Type guard
def is_int_index(k) -> bool:
return isinstance(k, int) and not isinstance(k, bool) Try / catch
try:
fib_k = fibonacci(k)
except TypeError:
fib_k = fibonacci(int(k)) Prevention
- Convert JSON/CLI values to int at the boundary of your program.
- Remember bool passes isinstance(x, int); exclude it explicitly if it matters.
- Large k in this naive recursion is exponential — memoize before worrying about types.
When it happens
Trigger: fibonacci('a'); fibonacci(10.0); fibonacci_search internals are safe because they only pass ints, so this error almost always comes from calling the helper directly with a value parsed from JSON/config (where numbers arrive as float or string).
Common situations: json.load produces floats for values like 10.0; CLI args passed as strings; numpy scalar types (np.int64 is fine on most builds but np.float64 is not an int).
Related errors
- k integer must be greater or equal to zero.
- '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/724cef7a2e89d6fa.
Report an issue: GitHub.