簡體   English   中英

類型錯誤:元組索引必須是整數,而不是元組

[英]Type error: Tuple indices must be integers, not tuple

我正在嘗試使這種方法起作用,但不會。

相關代碼:

for (i, t) in enumerate(transitions[location]):
    print i+1, t[0]
actionChoice=int(raw_input("> "))
console.clear()
transitions=transitions[location][actionChoice-1]

我收到類型錯誤:元組索引必須是整數,而不是元組

我應該在哪里修理? 這是什么意思?

location是一個元組。 此行導致錯誤: transitions[location]

還要注意, enumerate接受start參數,因此您可以使用enumerate(x, start=1)避免寫入i+1

這是一個演示:

正確:

>>> tup=(1,2)
>>> tup[0]
1

不正確:

>>> tup[(0,0)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: tuple indices must be integers, not tuple
>>> tup[1,]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: tuple indices must be integers, not tuple

位置很可能是一個元組-不是整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM