Python has two data types that represent numbers: floats and integers. These data types have distinct properties.
If you try to use a float with a function that only supports integers, like the range()
function, you’ll encounter the “TypeError: ‘float’ object cannot be interpreted as an integer” error.
In this guide we explain what this error message means and what causes it. We’ll walk through an example of this error so you can figure out how to solve it in your program.
TypeError: ‘float’ object cannot be interpreted as an integer
Floating-point numbers are values that can contain a decimal point. Integers are whole numbers. It is common in programming for these two data types to be distinct.
In Python programming, some functions like range() can only interpret integer values. This is because they are not trained to automatically convert floating point values to an integer.
This error is commonly raised when you use range()
with a floating-point number to create a list of numbers in a given range.
Python cannot process this because Python cannot create a list of numbers between a whole number and a decimal number. The Python interpreter would not know how to increment each number in the list if this behavior were allowed.
An Example Scenario
We’re going to build a program that calculates the total sales of cheeses at a cheesemonger in the last three months. We’ll ask the user how many cheeses they want to calculate the total sales for. We’ll then perform the calculation.
Start by defining a list of dictionaries with information on cheese sales:
cheeses = [ { "name": "Edam", "sales": [200, 179, 210] }, { "name": "Brie", "sales": [142, 133, 135] }, { "name": "English Cheddar", "sales": [220, 239, 257] } ]
Next, we ask the user how many total sales figures they want to calculate:
total_sales = float(input("How many total sales figures do you want to calculate? "))
We convert the value the user inserts to a floating-point. This is because the input() method returns a string and we cannot use a string in a range()
statement.
Next, we iterate over the first cheeses in our list based on how many figures the cheesemonger wants to calculate. We do this using a for loop and a range()
statement:
for c in range(0, total_sales): sold = sum(cheeses[c]["sales"]) print("{} has been sold {} times over the last three months.".format(cheeses[c]["name"], sold))
Our code uses the sum() method to calculate the total number of cheeses sold in the “sales” lists in our dictionaries.
Print out how many times each cheese has been sold to the console. Our loop runs a number of times equal to how many sales figures the cheesemonger has indicated that they want to calculate.
Run our code and see what happens:
How many total sales figures do you want to calculate? 2 Traceback (most recent call last): File "main.py", line 9, in <module> for c in range(0, total_sales): TypeError: 'float' object cannot be interpreted as an integer
Our code asks us how many figures we want to calculate. After we submit a number, our code stops working.
The Solution
The problem in our code is that we’re trying to create a range using a floating-point number. In our code, we convert “total_sales” to a float. This is because we need a number to create a range using the range()
statement.
The range()
statement only accepts integers. This means that we should not convert total_sales to a float. We should convert total_sales to an integer instead.
To solve this problem, change the line of code where we ask a user how many sales figures they want to calculate:
total_sales = int(input("How many total sales figures do you want to calculate? "))
We now convert total_sales to an integer instead of a floating-point value. We perform this conversion using the int() method.
Let’s run our code:
How many total sales figures do you want to calculate? 2 Edam has been sold 589 times over the last three months. Brie has been sold 410 times over the last three months.
Our code successfully calculates how many of the first two cheeses in our list were sold.
Conclusion
The “TypeError: ‘float’ object cannot be interpreted as an integer” error is raised when you try to use a floating-point number in a place where only an integer is accepted.
This error is common when you try to use a floating-point number in a range()
statement. To solve this error, make sure you use integer values in a range()
statement, or any other built-in statement that appears to be causing the error.
You now have the skills and know-how you need to solve this error like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.