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类型

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值