Python can only iterate over an iterable object. This is a type of object that can return its members one at a time. If you try to iterate over a non-iterable object, like a floating-point number, you see an error that says “TypeError: ‘float’ object not iterable”.
In this guide, we talk about what this error means and why you may encounter it. We walk through an example to help you understand how to resolve this error in your code.
TypeError: ‘float’ object not iterable
Iterable objects include list, strings, tuples, and dictionaries. When you run a for loop on these data types, each value in the object is returned one by one.
Numbers, such as integers and floating points, are not iterable. There are no members in an integer or a floating-point that can be returned in a loop.
The result of the “TypeError: ‘float’ object not iterable” error is usually trying to use a number to tell a for loop how many times to execute instead of using a range()
statement.
An Example Scenario
Here, we write a program that calculates the factors of a number. To start, we ask the user to insert a number whose factors they want to calculate:
factor = float(input("Enter a number: "))
The input()
method returns a string. We cannot perform mathematical operations on a string. We’ve used the float() method to convert the value returned by input()
to a floating-point number so that we can use it in a math operation.
Next, we write a for loop that calculates the factors of our number:
for n in factor: if factor % n == 0: print("{} is a factor of {}.".format(factor, n))
This loop should go through every number until it reaches “factor”.
This loop uses the modulo operator to check whether there is a remainder left after dividing the factor by “n”, which is the number in an iteration of our loop. If there is a remainder, “n” is not a factor. If there is a remainder, our “if” statement executes and prints a message to the console.
Run our code and see what happens:
Enter a number: 8 Traceback (most recent call last): File "main.py", line 3, in <module> for n in factor: TypeError: 'float' object is not iterable
Our code returns a TypeError.
The Solution
In our example above, we’ve tried to iterate over “factor”, which is a float. We cannot iterate over a floating-point number in a for loop. Instead, we should iterate over a range of numbers between 1 and the number whose factor we want to calculate.
To fix our code, we need to use a range() statement. The range()
statement generates a list of numbers in a specified range upon which a for loop can iterate. Let’s revise our for loop to use a range()
statement:
for n in range(1, int(factor) + 1): if factor % n == 0: print("{} is a factor of {}.".format(factor, n))
Our range()
statement generates a list of numbers between one and the value of “factor” plus one.
We have added 1 to the upper end of our range so that our “factor” number is considered a factor. Our for loop can then iterate over this list. We’ve converted “factor” to an integer in our code because the range()
statement does not accept floating point numbers.
Run our code again and see what happens:
8.0 is a factor of 1. 8.0 is a factor of 2. 8.0 is a factor of 4. 8.0 is a factor of 8.
Our code successfully returns a list of all the factors of the number we inserted into our program.
Conclusion
The Python “TypeError: ‘float’ object not iterable” error is caused when you try to iterate over a floating point number as if it were an iterable object, like a list or a dictionary.
To solve this error, use a range()
statement if you want to iterate over a number. A range statement generates a list of numbers in a given range upon which a for loop can iterate.
Now you’re ready to fix this common Python error in your code!
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.