如何修复:TypeError 'tuple' 对象不支持项目分配 [英] How to fix a : TypeError 'tuple' object does not support item assignment

查看:87
本文介绍了如何修复:TypeError 'tuple' 对象不支持项目分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本教程中的以下代码片段:http://www.raywenderlich.com/24252/beginning-game-programming-for-teens-with-python

The following fragment of code from this tutorial: 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)

正在给我一个:

TypeError: 'tuple' 对象不支持项目分配

TypeError: 'tuple' object does not support item assignment

我知道这可能是因为 badguy 是一个元组.这意味着它是不可变的(你不能改变它的值)我试过以下:

I understand that this could be becuse badguy is a tuple. 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

我将元组转换为列表,这样我们就可以减去 7.但在游戏中没有任何反应.

I converted the tuple to a list so we can minus 7. But in the game nothing happens.

有人知道我能做什么吗?

Does any one know what I could do?

谢谢.

推荐答案

改变这个

badguy[0]-=7

进入这个

badguy = list(badguy)
badguy[0]-=7
badguy = tuple(badguy)

或者,如果您可以将 badguy 保留为 list,那么甚至不要使用元组,并且您可以使用当前的代码(添加更改使用列表而不是元组)

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)

这篇关于如何修复:TypeError 'tuple' 对象不支持项目分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆