[412]TypeError: ‘tuple’ object does not support item assignment

def change(tupleTest):
    tupleTest[0] = 2
tupleTest = (1, 2, 3)
change(tupleTest)
print(tupleTest)

The above code explodes the problem error
the solution is actually easier to understand

Tuple is only readable, and does not support writing. Therefore, there is a problem with tuple assignment

Generally, if you want to do operations similar to arrays in C/C + +, you’d better use list
instead of the following code to avoid similar errors

def change(tupleTest):
    tupleTest[0] = 2
tupleTest = [1, 2, 3]
change(tupleTest)
print(tupleTest)

Although this name is still very unreliable
a suggestion for beginners of Python is that the name of the code is very important. Python is typeless (the type is not displayed obviously…)

Similar Posts: