[英]How to fix a : TypeError 'tuple' object does not support item assignment
The following fragment of code from this tutorial: http://www.raywenderlich.com/24252/beginning-game-programming-for-teens-with-python本教程中的以下代码片段: http : //www.raywenderlich.com/24252/beginning-game-programming-for-teens-with-python
for badguy in badguys:
if badguy[0]<-64:
badguys.pop(index)
badguy[0]-=7
index+=1
for badguy in badguys:
screen.blit(badguyimg, badguy)
is giving me a :正在给我一个:
TypeError: 'tuple' object does not support item assignment
类型错误:“元组”对象不支持项目分配
I understand that this could be becuse badguy
is a tuple.我知道这可能是
badguy
是一个元组。 This means it is immutable(you can not change its values) Ive tried the following:这意味着它是不可变的(你不能改变它的值)我尝试了以下方法:
t= list(badguy)
t[0]= t[0]-7
i+=1
I converted the tuple to a list so we can minus 7. But in the game nothing happens.我将元组转换为一个列表,这样我们就可以减去 7。但在游戏中什么也没有发生。
Does any one know what I could do?有谁知道我能做什么?
Thanks.谢谢。
Change this改变这个
badguy[0]-=7
into this进入这个
badguy = list(badguy)
badguy[0]-=7
badguy = tuple(badguy)
Alternatively, if you can leave badguy
as a list
, then don't even use tuples and you'll be fine with your current code (with the added change of using lists instead of tuples)或者,如果您可以将
badguy
为list
,那么甚至不要使用元组,并且您可以使用当前的代码(增加了使用列表而不是元组的更改)
Another solution is instead of另一种解决方案是代替
badguy[0] -= 7
to do做
badguy = (badguy[0] - 7,) + badguy[1:]
This creates a new tuple altogether with the updated value in the zeroth element.这与第零个元素中的更新值一起创建了一个新元组。
你可以做一个 np.copy() 并与她一起工作。
badguy_copy = np.copy(badguy)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.