这段代码是一个更大的pygame程序的一部分,这用于向下降的小行星的Y值加1。 它首先用y值= 2打印列表中的项目,但在第二个循环中,我收到错误消息。
GeneratedX = [] x = (200,1) y = (168,1) GeneratedX.append(x) GeneratedX.append(y) i = True while i == True: for current in GeneratedX: GeneratedX = (current[0],current[1] + 1) print(GeneratedX)This code is part of a bigger pygame program, and this is being used to add 1 to the Y value of a falling asteroid. It first prints the correct answer with the items in the list with y value = 2, but on the second loop, i get the error message.
GeneratedX = [] x = (200,1) y = (168,1) GeneratedX.append(x) GeneratedX.append(y) i = True while i == True: for current in GeneratedX: GeneratedX = (current[0],current[1] + 1) print(GeneratedX)最满意答案
在第二次迭代中, GeneratedX不再是元组列表,而是一个元组 ( GeneratedX = (current[0],current[1] + 1) )。
这就是为什么current是一个int并且current[0]引发TypeError 。
一个可能的解决方案是按以下方式使用enumerate :
for i, current in enumerate(GeneratedX): GeneratedX[i] = (current[0], current[1] + 1)与其余代码输出一起使用时:
[(200, 2), (168, 1)] [(200, 2), (168, 2)] [(200, 3), (168, 2)] [(200, 3), (168, 3)] [(200, 4), (168, 3)] [(200, 4), (168, 4)] [(200, 5), (168, 4)] [(200, 5), (168, 5)] [(200, 6), (168, 5)] . . . .In the second iteration GeneratedX is no longer a list of tuples, instead it is a tuple (GeneratedX = (current[0],current[1] + 1)).
That's why current is an int and current[0] raises the TypeError.
A probable solution would be to use enumerate in the following fashion:
for i, current in enumerate(GeneratedX): GeneratedX[i] = (current[0], current[1] + 1)When used with the rest of your code outputs:
[(200, 2), (168, 1)] [(200, 2), (168, 2)] [(200, 3), (168, 2)] [(200, 3), (168, 3)] [(200, 4), (168, 3)] [(200, 4), (168, 4)] [(200, 5), (168, 4)] [(200, 5), (168, 5)] [(200, 6), (168, 5)] . . . .TypeError:'int'对象不可下标,在循环中添加到元组中(TypeError: 'int' object is not subscriptable, adding to a tuple in a loop)这段代码是一个更大的pygame程序的一部分,这用于向下降的小行星的Y值加1。 它首先用y值= 2打印列表中的项目,但在第二个循环中,我收到错误消息。
GeneratedX = [] x = (200,1) y = (168,1) GeneratedX.append(x) GeneratedX.append(y) i = True while i == True: for current in GeneratedX: GeneratedX = (current[0],current[1] + 1) print(GeneratedX)This code is part of a bigger pygame program, and this is being used to add 1 to the Y value of a falling asteroid. It first prints the correct answer with the items in the list with y value = 2, but on the second loop, i get the error message.
GeneratedX = [] x = (200,1) y = (168,1) GeneratedX.append(x) GeneratedX.append(y) i = True while i == True: for current in GeneratedX: GeneratedX = (current[0],current[1] + 1) print(GeneratedX)最满意答案
在第二次迭代中, GeneratedX不再是元组列表,而是一个元组 ( GeneratedX = (current[0],current[1] + 1) )。
这就是为什么current是一个int并且current[0]引发TypeError 。
一个可能的解决方案是按以下方式使用enumerate :
for i, current in enumerate(GeneratedX): GeneratedX[i] = (current[0], current[1] + 1)与其余代码输出一起使用时:
[(200, 2), (168, 1)] [(200, 2), (168, 2)] [(200, 3), (168, 2)] [(200, 3), (168, 3)] [(200, 4), (168, 3)] [(200, 4), (168, 4)] [(200, 5), (168, 4)] [(200, 5), (168, 5)] [(200, 6), (168, 5)] . . . .In the second iteration GeneratedX is no longer a list of tuples, instead it is a tuple (GeneratedX = (current[0],current[1] + 1)).
That's why current is an int and current[0] raises the TypeError.
A probable solution would be to use enumerate in the following fashion:
for i, current in enumerate(GeneratedX): GeneratedX[i] = (current[0], current[1] + 1)When used with the rest of your code outputs:
[(200, 2), (168, 1)] [(200, 2), (168, 2)] [(200, 3), (168, 2)] [(200, 3), (168, 3)] [(200, 4), (168, 3)] [(200, 4), (168, 4)] [(200, 5), (168, 4)] [(200, 5), (168, 5)] [(200, 6), (168, 5)] . . . .
发布评论