user2407771
user2407771

Reputation: 31

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?

Upvotes: 2

Views: 10124

Answers (2)

dawg
dawg

Reputation: 104102

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.

Upvotes: 0

jamylak
jamylak

Reputation: 133764

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

Upvotes: 5

Related Questions