# Solution for the “TypeError: float object cannot be interpreted as an integer” in Python

If you are having trouble with the error message: “TypeError: float object cannot be interpreted as an integer” in Python, this article is what you need. This kind of error occurs when using a function that expects the integer argument but passes it a float value instead. We need to convert the variable to an integer value to overcome this problem. Keep reading for further information.

## What causes this error?

You will get the error message: “TypeError: float object cannot be interpreted as an integer” when using a function that expects the integer argument but passes it a float value instead.

This kind of error is commonly raised when you use the range() function with a floating-point number. In Python, some functions like ‘range()’ can only interpret integer values.

To illustrate, here is an example of how this error occurs:

coefficients = [1.86, 1.26, 2.2, 1.18, 2.0]
numOfCoff = len(coefficients)

for x in range(numOfCoff / 2):
print(coefficients[x])

Error:

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

In this situation, the error occurs because numOfCoff / 2 returns a float value – numOfCoff  equals to 5 so numOfCoff / 2 has a value of 2.5.

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

To overcome this problem, we need to convert the variable to an integer value instead of float value before passing it to the range() function or make sure to pass an integer value directly to the range() function.

We can convert a floating-point value to an integer by using the int() function.

coefficients = [1.86, 1.26, 2.2, 1.18, 2.0]
numOfCoff = len(coefficients)

# Convert 'numOfCoff' to an integer
for x in range(int(numOfCoff / 2)):
print(coefficients[x])

Output:

1.86
1.26

Alternatively, you can use the ‘//’ operator instead of the ‘/’ operator in the example above.

coefficients = [1.86, 1.26, 2.2, 1.18, 2.0]
numOfCoff = len(coefficients)

for x in range(numOfCoff // 2):
print(coefficients[x])

Output:

1.86
1.26

The error no longer occurs because now the ‘numOfCoff // 2’ division returns the value ‘2’ which is an integer

The // operator is called Floor Division. It performs division, where the result is the quotient after removing the digits after the comma. For example:

print(5/2)
print(5//2)

Output:

2.5
2

## Summary

To sum up, you must pass the integer value to the function that expects the integer argument to avoid getting the error “TypeError: float object cannot be interpreted as an integer” in Python. That’s the end of this article. Hopefully, with the examples we shared above, you were able to fix your problem.