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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s