Panagiotis
Panagiotis

Reputation: 511

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

My code:

for i in range( 3.3, 5 ):
        print( i )

The above code have to print:

3.300000

4.300000

but the interpreter of Python 3.4.0 printed the following error:

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

Upvotes: 0

Views: 4778

Answers (1)

stonesam92
stonesam92

Reputation: 4457

range() works with integers not floats, but you can build your own range generator which will do what you want:

def frange(start, stop, step=1):
    i = start
    while i < stop:
        yield i
        i += step

for i in frange(3.3, 5) will give you the desired result.

Note though, that frange will, unlike range but like xrange, return a generator rather than a list.

Upvotes: 1

Related Questions