[英]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. location
是一个元组。 This line causes the error: transitions[location]
此行导致错误:
transitions[location]
Also note that enumerate
accepts a start
parameter so you can use enumerate(x, start=1)
to avoid writing i+1
还要注意,
enumerate
接受start
参数,因此您可以使用enumerate(x, start=1)
避免写入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. 位置很可能是一个元组-不是整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.