1. 程式人生 > 其它 >python 報錯 TypeError: ‘int‘ object is not subscriptable 解決方法

python 報錯 TypeError: ‘int‘ object is not subscriptable 解決方法

報錯原因 整數上加了下標不是陣列 當作陣列 使用

錯誤情況1:

a = 4
c=a[2]

# 或者
a = 4
index=2
c=a[index]

報錯:line 2, in <module>
c=a[2]

TypeError: 'int' object is not subscriptable

錯誤情況2:

a = [1,2,3,4]
c=a[2][2]

本身是個一維陣列,卻取了一個數組下標後,再加下標,同樣的問題。