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
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.
来源:https://stackoverflow.com/questions/33355608/python-typeerror-float-object-cannot-be-interpreted-as-an-integer