Python TypeError:“float"对象不能解释为整数 [英] Python TypeError: 'float' object cannot be interpreted as an integer
本文介绍了Python TypeError:“float"对象不能解释为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码:
for i in range( 3.3, 5 ):
print( i )
上面的代码必须打印:
3.300000
4.300000
但是 Python 3.4.0 的解释器打印了以下错误:
but the interpreter of Python 3.4.0 printed the following error:
TypeError: 'float' 对象不能被解释为整数
TypeError: 'float' object cannot be interpreted as an integer
推荐答案
range()
适用于整数而不是浮点数,但您可以构建自己的范围生成器,它可以执行您想要的操作:>
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)
会给你想要的结果.
但请注意,frange
将与 range
不同,但与 xrange
一样,返回生成器而不是列表.
Note though, that frange
will, unlike range
but like xrange
, return a generator rather than a list.
这篇关于Python TypeError:“float"对象不能解释为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文