简体   繁体   中英

Type error: Tuple indices must be integers, not tuple

I am trying to get this method to work, but it won't.

Relevant code:

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

I get the Type Error: tuple indices must be integers, not tuple

Where should I fix it? What does it mean?

location is a tuple. This line causes the error: transitions[location]

Also note that enumerate accepts a start parameter so you can use enumerate(x, start=1) to avoid writing i+1

Here is a demo:

Correct:

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

Not correct:

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

Most probably, location is a tuple -- not an integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM