TypeError: float object cannot be interpreted as an integer

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Table of Contents

  1. TypeError: 'FLOAT' object cannot be interpreted as an integer
  2. TypeError: 'STR' object cannot be interpreted as an integer

# 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):.

typeerror float object cannot be interpreted as an integer

Here is an example of how the error occurs.

main.py
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.

Floating point numbers have one or more digits after the decimal, whereas integers don't have a decimal.

main.py
print(type(10.0)) # ๐Ÿ‘‰๏ธ <class 'float'> print(type(10.5)) # ๐Ÿ‘‰๏ธ <class 'float'> print(type(5)) # ๐Ÿ‘‰๏ธ <class 'int'> print(type(13)) # ๐Ÿ‘‰๏ธ <class 'int'>

# Use the floor division operator to solve the error

To solve the error, use the floor division operator // instead of the division / operator.

main.py
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)

use floor division operator to solve the error

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.

# Convert the float to an integer when calling the function

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

main.py
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

convert float to integer when calling function

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
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)
main.py
print(list(range(5))) # ๐Ÿ‘‰๏ธ [0, 1, 2, 3, 4] print(list(range(1, 5))) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4]

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

# Convert the float to an integer by rounding

You can also convert the float to an integer by rounding.

main.py
my_float = 3.51 for i in range(round(my_float)): print(i) # 0, 1, 2, 3 print(round(my_float)) # ๐Ÿ‘‰๏ธ 4

convert float to integer by rounding

The round() function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe number of digits after the decimal, the number should have after the operation (optional)

The round function returns the number rounded to ndigits precision after the decimal point.

If ndigits is omitted, the function returns the nearest integer.

You can use the math.ceil() or math.floor() method if you need to round the float up or down.

main.py
import math my_float = 3.51 rounded_up = math.ceil(my_float) print(rounded_up) # ๐Ÿ‘‰๏ธ 4 rounded_down = math.floor(my_float) print(rounded_down) # ๐Ÿ‘‰๏ธ 3

using math ceil and math floor

The math.ceil() method returns the smallest integer greater than or equal to the provided number.

The math.floor() method returns the largest integer less than or equal to the provided number.

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.

# Using numpy.arange to solve the error

The native range() function cannot be called with a floating-point number, but you can call the numpy.arange method with one.

main.py
import numpy as np my_float = 3.5 for i in np.arange(my_float): # 0.0 # 1.0 # 2.0 # 3.0 print(i)

using numpy arange method to solve the error

The numpy.arange() method takes the start, stop and step values as parameters, just like the range() function.

However, numpy.arange handles floating-point values.

If you need to install numpy, open your terminal in your project's root directory and run the following command.

shell
pip install numpy pip3 install numpy

# Reassigning a variable by mistake

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

main.py
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)

reassigning variable by mistake

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

# TypeError: 'str' object cannot be interpreted as an integer

The Python "TypeError: 'str' object cannot be interpreted as an integer" occurs when we pass a string to a function that expects an integer argument.

To solve the error, convert the string to an integer in the call to the function.

typeerror str object cannot be interpreted as an integer

Here is an example of how the error occurs.

main.py
# ๐Ÿ‘‡๏ธ this is a string my_num = '5' # โ›”๏ธ TypeError: 'str' object cannot be interpreted as an integer for i in range(my_num): print(i)

We passed a string to the range() constructor which expects an integer argument.

# Convert the string to an integer in the call to the function

To solve the error, use the int() class to convert the string to an integer.

main.py
my_num = '5' for i in range(int(my_num)): print(i)

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

main.py
print(int('5')) # ๐Ÿ‘‰๏ธ 5 (integer) print(int('3')) # ๐Ÿ‘‰๏ธ 3 (integer)

# The input() function always returns a string

The error often occurs when taking input from the user with the built-in input() function.

main.py
# ๐Ÿ‘‡๏ธ this is a string num = input('Enter your fav number: ') print(num) print(type(num)) # ๐Ÿ‘‰๏ธ <class 'str'> # โ›”๏ธ TypeError: 'str' object cannot be interpreted as an integer for i in range(num): print(i)

input function always returns string

The input function converts the supplied value to a string and returns it.

The input() function is guaranteed to return a string even if the user enters an integer.

Use the int() constructor to convert the value to an integer to solve the error.

main.py
num = input('Enter your fav number: ') print(num) print(type(num)) # ๐Ÿ‘‰๏ธ <class 'str'> # โœ… Convert str to integer for i in range(int(num)): print(i)

convert input to integer

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

The constructor returns 0 if no arguments are given.

main.py
print(int('10')) # ๐Ÿ‘‰๏ธ 10 print(int('5')) # ๐Ÿ‘‰๏ธ 5

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

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)
main.py
print(list(range(3))) # ๐Ÿ‘‰๏ธ [0, 1, 2] print(list(range(1, 4))) # ๐Ÿ‘‰๏ธ [1, 2, 3]

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

# Reassigning a variable to a string by mistake

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

main.py
my_int = 10 # ๐Ÿ‘‡๏ธ reassigned variable to a string by mistake my_int = '30' # โ›”๏ธ TypeError: 'str' 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 string.

In this case, you have to track down where the variable got set to a string and correct the assignment.

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

Copyright ยฉ 2024 Borislav Hadzhiev