TypeError: 'float' object is not iterable 解决方案

遇到了一个经典问题,需要记录一下,源代码如下:

def Deal(open_price, exit_price):
	all_price = open_price + exit_price
	for i in len(all_price)/2:
		price = all_price[n+1][1] - all_price[n][1]
		net.append(float(price, 2))
	print(net)

类中的某一个方法,在运行时,报错:

Traceback (most recent call last):
  File "111.py", line 147, in <module>
    main()
  File "111.py", line 143, in main
    Deal(open_price, exit_price)
  File "111.py", line 117, in Deal
    for i in len(all_price)/2:
TypeError: 'float' object is not iterable

for循环报 float类型错误???

我输出了all_price是列表类型,len(all_price)是int类型

最后:在len(all_price)/2那里,代码自动转换成了float类型

所以解决方案顺其自然的就变成要四舍五入后转换为int类型

猜你喜欢

转载自blog.csdn.net/qq_18999357/article/details/80785242