Last updated: Apr 8, 2024
Reading timeยท5 min
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.
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.
print(type(10.0)) # ๐๏ธ <class 'float'> print(type(10.5)) # ๐๏ธ <class 'float'> print(type(5)) # ๐๏ธ <class 'int'> print(type(13)) # ๐๏ธ <class 'int'>
To solve the error, use the
floor division operator //
instead of
the division /
operator.
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.
floor()
function applied to the result.Alternatively, you can convert the float to an int by passing it to the int()
constructor.
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:
Name | Description |
---|---|
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 ) |
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.
You can also convert the float to an integer by rounding.
my_float = 3.51 for i in range(round(my_float)): print(i) # 0, 1, 2, 3 print(round(my_float)) # ๐๏ธ 4
The round() function takes the following 2 parameters:
Name | Description |
---|---|
number | the number to round to ndigits precision after the decimal |
ndigits | the 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.
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
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.
int()
constructor.numpy.arange
to solve the errorThe native range()
function cannot be called with a floating-point number, but
you can call the numpy.arange
method with one.
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)
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.
pip install numpy pip3 install numpy
Make sure you aren't declaring a variable that stores an integer initially and overriding it somewhere in your code.
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.
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.
Here is an example of how the error occurs.
# ๐๏ธ 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.
To solve the error, use the int()
class to convert the string to an integer.
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.
print(int('5')) # ๐๏ธ 5 (integer) print(int('3')) # ๐๏ธ 3 (integer)
The error often occurs when taking input from the user with the built-in
input()
function.
# ๐๏ธ 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)
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.
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)
The int()
class returns an integer object constructed from the provided number
or string argument.
The constructor returns 0
if no arguments are given.
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:
Name | Description |
---|---|
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 ) |
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.
Make sure you aren't declaring a variable that stores an integer initially and overriding it somewhere in your code.
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.