HOWTO · Python
How to Fix the TypeError: 'float' Object Cannot Be Interpreted as an Integer in Python
Fix Python's TypeError: 'float' object cannot be interpreted as an integer by converting the float before passing it to `range()` with `int()`, `math.floor()`, or `math.ceil()` to match the intended iteration count.
On this page
If you see TypeError: 'float' object cannot be interpreted as an integer, a float is being passed where an integer is required. A common cause is giving a decimal value to range(). Convert the value according to the number of iterations you intend; choosing a conversion changes the loop count.
Why range() raises this TypeError for a float
range() creates a sequence of integers, so its start, stop, and step arguments must be integers (or integer-like objects that implement Python’s integer index protocol). A float such as 3.0 is not accepted, even though its fractional part is zero: it is still a float, not an int. For example, range(3.5) raises TypeError: 'float' object cannot be interpreted as an integer.
Python keeps integer and floating-point values distinct: 3 has type int, while 3.5 has type float. Arithmetic can produce a float even when a later loop needs a whole-number count. The error identifies that mismatch at the call to range(); it does not mean that Python cannot store or calculate with floats.
Convert a float to an integer before using range()
Convert the count before passing it to range(), and choose the conversion based on what the count represents. The built-in int() works without an import. It discards the fractional part toward zero, so int(3.7) is 3 and int(-3.7) is -3. It does not round to the nearest integer. If the value represents a measured quantity or a computed limit, decide whether truncating it matches the intended number of iterations.
The math.floor() and math.ceil() functions are available from Python’s standard-library math module, so no package installation is needed; use import math. math.floor() returns the greatest integer less than or equal to its input, while math.ceil() returns the least integer greater than or equal to it. For 3.7, floor is 3 and ceil is 4; for -3.7, floor is -4 and ceil is -3. This distinction matters when the converted number controls a loop: rounding down a negative value moves farther from zero, while int() moves toward zero.
Before changing a value, identify what it counts, what a partial unit means, and whether the bound should include the final partial unit. A retry limit, page count, or number of records can each need a different rule. Keep the original measurement available so conversion does not hide precision needed elsewhere. The short program below applies a chosen whole-number count to an ordinary sequence.
Run this example:
import math
count = 3.7
truncated = int(count)
rounded_up = math.ceil(count)
print("int:", truncated)
print("ceil:", rounded_up)
for index in range(truncated):
print(index)
int: 3
ceil: 4
0
1
2
Choose int(), math.floor(), or math.ceil() based on the result you need
Use int() when removing the fractional part toward zero is the intended rule. Use math.floor() when the count must not exceed the original value, and math.ceil() when it must be at least the original value. For positive inputs, int() and math.floor() agree; for negative non-integers, they differ. Neither choice is universally correct: consider whether the float is a duration, a quantity to process, or a bound, then choose the rule that gives the desired count.
All three conversions produce an integer for a finite float, making the result suitable for range(). They also discard fractional information, so retain the original float separately if later calculations still need it. If the value is already meant to be an integer count, it may be better to fix its source or validate it before conversion rather than silently changing the count.
Check the converted value and loop output
Inspect the converted count as well as the loop’s values. range(n) starts at 0, advances by 1, and stops before n; a count of 3 therefore produces 0, 1, and 2, for three iterations. A count of 0 produces no iterations, and a negative stop value also produces no values when the default positive step is used. These behaviors can reveal that a conversion changed the work your program performs.
In the example, the printed count and loop values make the effect of truncating 3.7 visible. To check another input, replace count and compare the conversion with your intended rule. For a negative input, compare int(count), math.floor(count), and math.ceil(count) before using one as the stop value. The right fix is the conversion whose result matches the desired iteration count, not simply any conversion that makes the TypeError disappear.