强制转换为int而不能在Python中工作
问题描述:
我试图将一个时间值存储在一个数组中,但每次都会给我一个错误,即使我尝试将float转换为int也是如此。这里是我的代码的部分:强制转换为int而不能在Python中工作
EndTimes = [0,0,0,0]
...
EndTimes[TakenSlots] = int(time.time() + n)
,但它只是给出了这样的错误:
[error] TypeError (list indices must be integers)
[error] --- Traceback --- error source first
line: module (function) statement
44: main (craftTime) EndTimes[TakenSlots] = tempInt
我想这个代码只是为了看看它认为的价值是:
tempInt = int(time.time() + n)
print tempInt
EndTimes[TakenSlots] = tempInt
它只是输出1412046180(没有小数位,这似乎应该是一个int)
有谁知道w帽子发生了吗?它是int()还是我使用的数组类型的问题?提前致谢!
答
发生这种情况是因为列表索引(list [index] = value)必须是整数。 TakenSlots可能不是一个整数。
>>> l = [1,2,3]
>>> l[1.3] = 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not float
+0
糟糕,你是完全正确的!我之前改变了我的变量,完全忘了。谢谢您的帮助! – user2817653 2014-09-30 04:01:57
您能告诉我们完整的代码吗? – 2014-09-30 03:07:08