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

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

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

# Solution:
# This error usually comes up if you're trying to use floats in a 
# function/context that can only use integers. A common example is
# trying to use floats in the range() function which only takes integers.

# A common way to get around this with range() is to use a multiple of
# the float series you want and then to divide the resulting numbers
# inside your loop. E.g.:
for i in range(10):
  print(i/10)
--> 0.0
	0.1
	0.2
	0.3
	0.4
	0.5
	0.6
	0.7
	0.8
	0.9

# This way you can get the floats 0.0 - 0.9 without using floats in range

Example 2: numpy.float64' object cannot be interpreted as an integer

false:
np.zeros(np.float(3))
Right:
np.zeros(np.int(np.float(3)))

Tags:

Misc Example