TypeError: 'float' object cannot be interpreted as an integer (tested fix)
TypeError: 'float' object cannot be interpreted as an integer means a float reached something that needs a whole-number count, most often range(). We reproduced it in Python 3.13 and compared the fixes, which round differently.
1. / always returns a float
range(), str.zfill() and other functions that need a count only accept integers. The float usually comes from /, which returns a float even when the result is whole (10 / 2 is 5.0). We passed that 5.0 to six places that need an int. Note that the error message differs by function:
n = 10 / 2 # / always returns a float, even for whole numbers
print(n, type(n).__name__)
calls = ["range(n)", "'ab' * n", "[0] * n", "list('abc')[n:]", "bytes(n)", "'x'.zfill(n)"]
for c in calls:
try:
eval(c); print(f"{c:18} OK")
except TypeError as e:
print(f"{c:18} TypeError: {e}")
Output:
5.0 float
range(n) TypeError: 'float' object cannot be interpreted as an integer
'ab' * n TypeError: can't multiply sequence by non-int of type 'float'
[0] * n TypeError: can't multiply sequence by non-int of type 'float'
list('abc')[n:] TypeError: slice indices must be integers or None or have an __index__ method
bytes(n) TypeError: cannot convert 'float' object to bytes
'x'.zfill(n) TypeError: 'float' object cannot be interpreted as an integer
2. The fix: // or an explicit conversion
Use floor division // when you want an integer result from ints. If the value is already a float, convert it on purpose and choose how to round. int() truncates toward zero, round() rounds to nearest (ties go to the even number: round(2.5) is 2, round(7.5) is 8), math.ceil/math.floor go up or down:
total, parts = 10, 4
print(range(total // parts)) # floor division keeps ints
print(range(int(7.9)), range(round(7.9)), range(int(-7.9)))
import math
print(math.ceil(10 / 4), math.floor(10 / 4))
Output:
range(0, 2)
range(0, 7) range(0, 8) range(0, -7)
3 2
Note int(7.9) is 7, not 8, and int(-7.9) is -7, not -8. Pick the one that matches what the number means (for example, "how many pages do I need" is math.ceil).
3. Only accept whole floats
If a float like 3.0 is fine but 2.5 is a bug, check with is_integer() before converting rather than silently truncating:
for v in [3.0, 2.5]:
print(v, v.is_integer())
x = 3.0
if x.is_integer():
print(list(range(int(x))))
Output:
3.0 True
2.5 False
[0, 1, 2]
The same error from NumPy (np.linspace(0, 1, num) with a float num) has the same fix, int(num). We didn't run NumPy for this page. If the value is None rather than a float, see 'NoneType' object cannot be interpreted as an integer.
Frequently asked questions
How do I fix 'float' object cannot be interpreted as an integer in range()?
Pass an int: use // instead of / when dividing, or convert with int(), round() or math.ceil() depending on how you want to round.
Why is my number a float when it's a whole number?
The / operator always returns a float in Python 3: 10 / 2 is 5.0. Use 10 // 2 for 5.
Does int() round a float?
No, it truncates toward zero: int(7.9) is 7 and int(-7.9) is -7. Use round() for nearest (ties go to even: round(2.5) is 2), math.ceil() to round up.
Written with AI assistance. Measured on 2026-10-22 on Debian with Python 3.13.5: a float from / passed to range, sequence repetition, slicing, bytes and zfill, and the //, int, round, ceil, floor and is_integer fixes. Every block was run verbatim and its output pasted unedited. NumPy was not tested. PlainHeaders is our extension; Python is free.