How to Fix: TypeError: ‘float’ object is not iterable

3D illustration of a robot failing to peel a solid metal sphere, representing the float object is not iterable error.

This error is a direct cousin of TypeError: ‘int’ object is not iterable. It means: “You tried to use a for loop on a single decimal number, which is not allowed.” The TypeError float not iterable message indicates that you attempted to iterate over a float.

“Iterable” means something you can loop over, one item at a time (like a list or a string). A single number, like 10.5, is not.

โšก Quick Fix: TypeError: ‘float’ object is not iterable โ€” Python range(int()) Fix for for Loop Over Decimal Number and numpy.arange() for Float Step Sequences

You pointed a for loop directly at a float โ€” floats are single values with no sequence, no positions, and no items to hand back one at a time.

# WRONG โ€” 10.5 is a single value, not a sequence of 10.5 steps
my_number = 10.5
for i in my_number:             # TypeError: 'float' object is not iterable
    print(i)

# WRONG โ€” range() rejects floats entirely
for i in range(10.5):           # TypeError: 'float' object cannot be interpreted as an integer
    print(i)

# FIX 1 โ€” loop an integer number of times: convert float to int first
for i in range(int(my_number)): # int(10.5) = 10, range(10) works
    print(f"Loop {i}")

# FIX 2 โ€” loop over the float itself in a list (when the value is the data, not the count)
prices = [10.5, 20.99, 5.0]
for price in prices:            # iterating a list of floats โ€” always safe
    print(price)

# FIX 3 โ€” float step sequences: use numpy.arange() or a list comprehension
import numpy as np
for val in np.arange(0.0, 1.0, 0.1):   # 0.0, 0.1, 0.2, ... 0.9
    print(round(val, 1))

The breakdown below covers the for-loop-on-a-float case, the range(float) case, and the float step sequence โ€” the three forms this error takes in real Python code.

The Cause

You are trying to loop on the number, not up to the number.

Problem Code:

my_number = 10.5
for i in my_number: # CRASH!
    print("This won't work")

Python doesn’t know how to loop “10.5” times.

The Fix: Use range() and int()

If you want to loop 10 times (the integer part of your number), you must:

  1. Convert the float to an integer using int().
  2. Pass that integer to the range() function.
my_number = 10.5

# Convert 10.5 to 10
loop_count = int(my_number)

# range(10) creates [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(loop_count):
    print(f"Loop number {i}")

Important: range() only works with whole numbers (integers). You can never give it a float.

# This will ALSO fail:
range(10.5) 
# TypeError: 'float' object cannot be interpreted as an integer

Always use int() to convert your number first.


TypeError: ‘float’ object is not iterable โ€” Three Causes and the Right Tool for Each

TypeError: ‘float’ object is not iterable fires the moment Python’s for loop calls iter() on your variable and receives a float. Floats implement no iteration protocol โ€” they have no positions, no sequence, no next item to yield.

Three causes generate this error. Identify yours in 30 seconds.

A float used as a loop target. for i in 10.5: attempts to iterate the value 10.5 itself. The fix depends on intent: if 10.5 is a count, convert with int() and wrap in range(). If 10.5 is a data point, put it in a list and iterate the list.

A float passed to range(). range() accepts only integers. range(10.5) fires a different error message โ€” TypeError: ‘float’ object cannot be interpreted as an integer โ€” but the fix is the same: int(10.5) gives 10, range(10) works.

A float step sequence where you need fractional increments. range(0, 1, 0.1) crashes โ€” range() has no float step support. Two alternatives exist. numpy.arange(0.0, 1.0, 0.1) produces a NumPy array of floats with exact step control. A list comprehension [i * 0.1 for i in range(10)] avoids NumPy entirely and gives the same result as plain Python.

All three solutions side by side

import numpy as np

Option 1 โ€” numpy.arange for clean float sequences

steps = np.arange(0.0, 1.0, 0.1)

Option 2 โ€” list comprehension, no dependencies

steps = [round(i * 0.1, 1) for i in range(10)]

Option 3 โ€” integer loop with inline float math

for i in range(10):
val = i * 0.1
print(round(val, 1))

The round() call prevents floating-point drift โ€” 0.1 * 3 in Python produces 0.30000000000000004 without rounding. Apply round() to every float computed from repeated multiplication and your output stays clean.

Similar Posts

Leave a Reply