Python float object cannot be interpreted as an integer

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.

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

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 
	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!

How do you convert float to int in Python?

Python also has a built-in function to convert floats to integers: int[] . In this case, 390.8 will be converted to 390 . When converting floats to integers with the int[] function, Python cuts off the decimal and remaining numbers of a float to create an integer.

How do you convert a float to an integer?

A float value can be converted to an int value no larger than the input by using the math. floor[] function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil[] function.

How do I fix TypeError float object is not iterable?

The Python "TypeError: 'float' object is not iterable" occurs when we try to iterate over a float or pass a float to a built-in function like, list[] or tuple[] . To solve the error, use the range[] built-in function to iterate over a range, e.g. for i in range[int[3.0]]: .

What is a float in Python?

Float[] is a method that returns a floating-point number for a provided number or string. Float[] returns the value based on the argument or parameter value that is being passed to it. If no value or blank parameter is passed, it will return the values 0.0 as the floating-point output.

Chủ Đề