TypeError: 'float' object cannot be interpreted as an integer (Fix)
Fix the Python error TypeError: 'float' object cannot be interpreted as an integer. This error happens when Python needs a whole number, but your code gives it a float instead.
This page shows what the error means, where beginners usually see it, and the safest ways to fix it.
Quick fix #
count = 5.0
for i in range(int(count)):
print(i)
Output:
0
1
2
3
4
Use int() only when converting to a whole number makes sense. If you need decimal values, change the logic instead of forcing a float into range() or another integer-only operation.
What this error means #
This error means:
- Python expected an integer value
- Your code gave a float instead
- An integer is a whole number like
3or10 - A float is a decimal number like
3.5or10.0
Even if a float looks like a whole number, such as 5.0, it is still a float type in Python.
If you are not sure about the difference, see Python numbers explained: int, float, complex.
Where beginners usually see this error #
Beginners often see this error in places where Python needs a count or a position, such as:
- Using
range()with a float - Passing a float as a list index
- Using a float in string repetition like
"a" * 2.5 - Using a float where Python needs a count or position
A very common example is the range() function, because range() only accepts integers.
Example that causes the error #
Here is a simple example:
for i in range(5.0):
print(i)
Result:
TypeError: 'float' object cannot be interpreted as an integer
Why this fails:
range(5.0)raises this error- Even
5.0is still a float, not an int - Python does not automatically treat every float as an integer
Fix 1: Convert the float to int #
Use int(value) if a whole number is acceptable.
count = 5.0
for i in range(int(count)):
print(i)
Output:
0
1
2
3
4
This works because int(5.0) becomes 5.
Be careful:
int(value)removes the decimal partint(5.9)becomes5, not6
Example:
value = 5.9
print(int(value))
Output:
5
If you want to learn more, see Python int() function explained.
Fix 2: Use floor division when creating counts #
A common cause of this error is division.
Normal division with / returns a float:
items = 10
count = items / 2
print(count)
print(type(count))
Output:
5.0
<class 'float'>
That can cause a problem here:
items = 10
for i in range(items / 2):
print(i)
This raises the same error because items / 2 produces a float.
In many counting cases, floor division with // is better:
items = 10
for i in range(items // 2):
print(i)
Output:
0
1
2
3
4
Why this helps:
/returns a float//returns a whole-number result in many counting situationsrange()needs an integer count
Fix 3: Check earlier math in your code #
Sometimes the failing line looks correct, but the variable already became a float earlier.
Example:
total = 12
groups = total / 3
print(groups)
print(type(groups))
for i in range(groups):
print(i)
Output before the error:
4.0
<class 'float'>
The problem started at this line:
groups = total / 3
A good debugging habit is to print the value and its type before the line that fails.
value = 8 / 2
print(value)
print(type(value))
print(isinstance(value, int))
print(int(value))
Output:
4.0
<class 'float'>
False
4
Useful debugging commands:
print(value)
print(type(value))
print(isinstance(value, int))
print(int(value))
Fix 4: Change the logic if decimals are required #
Do not force a float into an integer-only operation if decimals matter.
For example, if you want to step through decimal values, range() is the wrong tool.
This does not work:
for x in range(0.5, 3.0):
print(x)
Instead, use a while loop and update a float value:
x = 0.5
while x < 3.0:
print(x)
x += 0.5
Output:
0.5
1.0
1.5
2.0
2.5
Use this approach when decimal steps are part of the logic.
Beginner debugging steps #
If you see this error, follow these steps:
- Read the full traceback and find the exact line.
- Look for
range(), indexing, slicing, or repetition on that line. - Print the variable value.
- Print
type(variable). - Decide whether to convert the value or rewrite the logic.
Example:
count = 15 / 3
print(count)
print(type(count))
for i in range(count):
print(i)
After printing the type, you can decide whether:
int(count)is correct, orcountshould have been created differently, such as with//
For a broader step-by-step process, see how to debug Python code.
Common mistakes #
These are common reasons this error appears:
- Using
/instead of//beforerange() - Reading a number from
input()and converting it in a way that produces a float - Calculating loop counts with decimal math
- Using a float as a list position
- Assuming
5.0is the same as5in all Python operations
If you are working with user input, you may also need how to convert string to float in Python.
FAQ #
Why does range(5.0) fail if 5.0 looks like a whole number? #
Because 5.0 is still a float type. range() requires an integer, not a float.
Should I always fix this with int()? #
No. Use int() only if dropping the decimal part is correct for your program.
What is the difference between / and // in Python? #
/ returns a float. // does floor division and is often better when you need a whole-number count.
Can this error happen outside range()? #
Yes. It can also happen with indexing, slicing, repetition, and other places that require integer values.