TypeError: ‘float’ object cannot be interpreted as an integer

TypeError: ‘float’ object cannot be interpreted as an integer

Assign two variables with number

a = 12
b = 3

Use the variables in a for loop with range. You get TypeError because by default the values are taken as float and when you try to divide them using / , the interpreter throws datatype error

for i in range(0,a/b):
    print(i)

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

Use // to perform an integer division which will execute without error

for i in range(0,a//b):
    print(i)

0
1
2
3

Leave a Reply

%d bloggers like this: