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

ぃ、小莉子 提交于 2019-11-28 06:39:45

问题


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


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!