Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix a : TypeError 'tuple' object does not support item assignment

Tags:

python

pygame

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)

is giving me a :

TypeError: 'tuple' object does not support item assignment

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

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?

Thanks.

like image 636
Pro-grammer Avatar asked Oct 12 '13 19:10

Pro-grammer


People also ask

Does the tuple object support item assignment?

In this article, we will learn about the error TypeError: "tuple" object does not support item assignment. A tuple is a collection of ordered and unchangeable items as they are immutable. So once if a tuple is created we can neither change nor add new values to it.

How do you fix a tuple error in Python?

The Python "TypeError: 'tuple' object is not callable" occurs when we try to call a tuple as if it were a function. To solve the error, make sure to use square brackets when accessing a tuple at a specific index, e.g. my_tuple[0] .

How do I fix TypeError int object does not support item assignment?

Conclusion # The Python "TypeError: 'int' object does not support item assignment" occurs when we try to assign a value to an integer using square brackets. To solve the error, correct the assignment or the accessor, as we can't mutate an integer value.


1 Answers

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)

like image 127
inspectorG4dget Avatar answered Oct 19 '22 07:10

inspectorG4dget