# TypeError: float object cannot be interpreted as an integer Last updated: Apr 20, 2022

## TypeError: float object cannot be interpreted as an integer#

The Python "TypeError: 'float' object cannot be interpreted as an integer" occurs when we pass a float to a function that expects an integer argument. To solve the error, use the floor division operator, e.g. `for i in range(my_num // 5):`. Here is an example of how the error occurs.

main.py
```Copied!```my_num = 50

print(my_num / 5)  # 👉️ 10.0 (float)

# ⛔️ TypeError: 'float' object cannot be interpreted as an integer
for i in range(my_num / 5):
print(i)
``````

The division operator `/` always produces a float value but the `range()` function expects an integer.

To solve the error, we can use the floor division operator `//` instead.

main.py
```Copied!```my_num = 50

# ✅ using floor division
for i in range(my_num // 5):
print(i)

print(my_num / 5)  # 👉️ 10.0 (float)
print(my_num // 5)  # 👉️ 10 (int)

# ✅ or convert to int
print(int(10.0)) # 👉️ 10 (int)
``````

Division `/` of integers yields a float, while floor division `//` of integers results in an integer.

The result of using the floor division operator is that of a mathematical division with the `floor()` function applied to the result.

Alternatively, you can convert the float to an int by passing it to the `int` constructor.

main.py
```Copied!```my_num = 50

# ✅ convert float to int
for i in range(int(my_num / 5)):
print(i)

print(int(10.0))  # 👉️ 10
print(int(5.0))  # 👉️ 5
``````

The int class returns an integer object constructed from the provided number or string argument.

The constructor returns `0` if no arguments are given.

The range function is commonly used for looping a specific number of times in `for` loops and takes the following parameters:

NameDescription
`start`An integer representing the start of the range (defaults to `0`)
`stop`Go up to, but not including the provided integer
`step`Range will consist of every N numbers from `start` to `stop` (defaults to `1`)

If you only pass a single argument to the `range()` constructor, it is considered to be the value for the `stop` parameter.

If you are getting the error in a different scenario, you have to convert the float argument to an integer by passing it to the `int()` constructor.

Make sure you aren't declaring a variable that stores an integer initially and overriding it somewhere in your code.

main.py
```Copied!```my_int = 50

# 👇️ reassigned variable to a float by mistake
my_int = 5.6

# ⛔️ TypeError: 'float' object cannot be interpreted as an integer
for i in range(my_int):
print(i)
``````

We initially set the `my_int` variable to an integer but later reassigned it to a float which caused the error.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer. You can use the search field on my Home Page to filter through all of my articles.