Write Python. Understand every error.

Open the editor

Error guide · TypeError

TypeError: 'float' object cannot be interpreted as an integer

range() was given a decimal number, but it needs a whole number.

Example

n = 5 / 2
for i in range(n):
    print(i)

What Python says

TypeError: 'float' object cannot be interpreted as an integer

As printed by Python 3.12. Other versions may word it slightly differently.

Try this code in Py-CRAFT

Why it happens

The division operator / always gives a float, even when the answer looks whole: 10 / 2 is 5.0. range() counts in whole steps, so it refuses 2.5 and also 5.0. Use // for whole-number division, or convert with int().

How to fix it

n = 5 // 2
for i in range(n):
    print(i)

Output: 0 ⏎ 1

Tips

Learn the ideas behind it

Other common errors

Share this page: