2014-09-30 45 views
0

我試圖將一個時間值存儲在一個數組中,但每次都會給我一個錯誤,即使我嘗試將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()還是我使用的數組類型的問題?提前致謝!

+0

您能告訴我們完整的代碼嗎? – 2014-09-30 03:07:08

回答

1

發生這種情況是因爲列表索引(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