简体   繁体   English

如何修复:TypeError 'tuple' 对象不支持项目分配

[英]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)或者,如果您可以将badguylist ,那么甚至不要使用元组,并且您可以使用当前的代码(增加了使用列表而不是元组的更改)

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.

相关问题 TypeError:“元组”对象不支持项目分配 - TypeError: 'tuple' object does not support item assignment TypeError:“元组”对象不支持项目分配在非元组对象上 - TypeError: 'tuple' object does not support item assignment On non tuple object 如何修复我的:TypeError:&#39;int&#39; 对象不支持项目分配 - How to fix my: TypeError: 'int' object does not support item assignment 如何修复“TypeError: 'NoneType' object 不支持项目分配” - How To Fix “TypeError: 'NoneType' object does not support item assignment” 如何修复 TypeError:&#39;NoneType&#39; 对象不支持项目分配 - how to fix TypeError: 'NoneType' object does not support item assignment 如何修复&#39;TypeError:&#39;str&#39;对象不支持项目赋值&#39; - How to fix 'TypeError: 'str' object does not support item assignment' 列表 object 上的“TypeError: 'tuple' object 不支持项目分配” - “TypeError: 'tuple' object does not support item assignment” on a list object TypeError:“元组”对象不支持字典中的项目分配 - TypeError: 'tuple' object does not support item assignment in dictionary Leetcode 3Sum TypeError: tuple object 不支持项赋值 - Leetcode 3Sum TypeError: tuple object does not support item assignment TypeError: 'tuple' object 在交换值时不支持项目分配 - TypeError: 'tuple' object does not support item assignment when swapping values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM