Python TypeError: 'float' object is not iterable Solution

Posted in /  

Python TypeError: 'float' object is not iterable Solution
vinaykhatri

Vinay Khatri
Last updated on November 22, 2024

    In Python, we have some iterable objects such as a string, list, tuple, dictionary, and set. The one property of these iterable objects is we can perform for loop over them and access their individual elements one by one. We can use the for loop and iterate over these iterable objects. There are many other functions, such as map, range, and filter, which also return iterable objects. But if we try to perform an iteration on a floating-point number, we will receive the error TypeError: 'float' object is not iterable .

    This Python tutorial will discuss this error in detail and see why it occurs and how to debug it. We will also walk through a common example of when many python developers encounter this error. So let's get started with the Error Statement.

    Python Error: TypeError: 'float' object is not iterable

    The Error statement TypeError: 'float' object is not iterable has two parts Exception Type and Error message.

    1. TypeError (Exception Type)
    2. 'float' object is not iterable (Error Message)

    1. TypeError

    TypeError is a standard Python error. It is raised when we try to perform an invalid or unsupported operation on a Python object.

    2.  'float' object is not iterable

    This is the error message telling us that we are performing an iteration operation on a floating-point object, and Python does not support iteration on a floating-point number.

    Example

    float_num = 30.0        #initialize a float number object
    
    #iterate over a float number(error)
    for num in float_num:
        print(num)

    Output

    Traceback (most recent call last):
      File "main.py", line 4, in 
        for num in float_num:
    TypeError: 'float' object is not iterable

    Break the code

    In this example, we are getting this error because we are trying to loop over the float_num variable, which is a floating-point number. And Python for loop can not iterate over a float object, so the interpreter threw the error.

    Solution

    There can be two case scenarios with the above example, we can either want to iterate over the digits of the float number, or we want to create a range of numbers from 0 up to the float number float_num . If we want to iterate over every digit of the float_num we can convert the object into a string using the str function and iterate over every digit.

    Solution 1 (Iterate over every digit)

    float_num = 30.0
    
    for digit in str(float_num):
        print(digit)

    Output

    3
    0
    .
    0

    If we want to print the range of numbers from o to float_num we first need to convert the float num object to an integer using int() function then use that int number into the range() function so the for loop can iterate over the iterable object returned by the range() function.

    We can not use the float number as an argument value to the range() function, because range function only accepts int value.

    Solution 2 (Iterate over the range from 0 to float_num )

    # initialize the float num
    float_num = 30.0
    
    # convert the float num into integer
    int_num = int(float_num)       #30
    
    for num in range(int_num):
        print(num, end="->")

    Output

    0->1->2->3->4->5->6->7->8->9->10->11->12->13->14->15->16->17->18->19->20->21->22->23->24->25->26->27->28->29->

    Common Example Scenario

    Many Python learners encounter this error when they directly use the float number with the for loop and forget to use the range() function. However, the range() the function also does not work with floating-point numbers because it only accepts integer numbers as argument values. Still, many new python learners commit the mistake of using the floating-point number with for loop and encounter the  TypeError: 'float' object is not iterable Error.

    Example

    Let's create a Python program that tells if the entered number is a Prime or not.

    # ask the user to enter a number
    number = float(input("Enter a number: "))
    
    for i in number:
        # check if the number is not a prime
        if number%i==0:
            print(f"{number} is not prime")
            break
    # if the for loop run completly
    else:
        print(f"{number} is a prime number")

    Output

    Enter a number: 5
    Traceback (most recent call last):
      File "main.py", line 4, in 
        for i in number:
    TypeError: 'float' object is not iterable

    Break the code

    In the above example, we are getting this error because in line 4, we are performing the for loop on the number object, which is the number entered by the user and converted into float using the float() function.

    Solution

    To solve the above problem, we need to take care of three things.

    1. First, convert the user input into int before using it in the for a loop.
    2. Second, use the range function instead of the integer or float when dealing with for loop.
    3. Third, we need to start the range function from 2 to user entered number . Because inside the for loop, we are performing modulo operation, and behind the scene, modulo operator use division operation, and when it tries do divide any number with 0, it returns the ZeroDivision Error.

    Solution

    # ask the user to enter a number and conver it into int
    number = int(input("Enter a number: "))
    
    for i in range(number):
        # check if the number is not a prime
        if number%i==0:
            print(f"{number} is not prime")
            break
    # if the for loop run completly
    else:
        print(f"{number} is a prime number")

    Output

    Enter a number: 79
    79 is a prime number

    Wrapping Up!

    Now let's wrap up our article “TypeError: ‘float’ object not iterable” Solution. In this article, we discussed what this error is in Python and how to solve it. This error occurs when we perform the iteration operation or for loop on a Python floating-point number. This is a TypeError exception because for loop operation is not supported by floating-point numbers. Mostly this error is raised when the user forgets to put the range function and directly apply the for loop on a floating number.

    If you are still getting this error in your program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments