TypeError: 'int' object does not support item assignment

python, typeerror

Solution

You are presumably trying to build a list of length `lenp` here. You'd need to create a list by multiplication here:

Temp = [None] * lenp

but you'd be better off building the list by appending to it:

Temp = []
for i in p:
    Temp.append(T * (i / po) ** rp)

where you don't use `p[i]` but `i` directly; Python `for` loops are for each loops really.

Your `range()` produces values in the series `[0, 10, 20, ... 1200)` and the `for` loop assigns each of those values to `i` per iteration. If you use `i` to index into `p` again you'd run into problems; `p[0]` would still be `0`, but `p[10]` would then be `100`, `p[20]` is `200`, etc. until `p[120]` throws an `IndexError` because there are only 119 different values in that range.

You can collapse the `for` loop appending to `Temp` into a list comprehension to build the list in one go:

rp=1.331
po=1000
T=280

Temp = [T * (i / po) ** rp for i in range(0, 1200, 10)]

Problem

``` p=range(0,1200,10) lenp=len(p) rp=1.331 po=1000 T=280 Temp=(lenp) for i in p: Temp[i]=T*(p[i]/po)**rp print T ``` Im getting this error and don't know how to fix it... ``` Temp[i]=T*(p[i]/po)**rp TypeError: 'int' object does not support item assignment ```

Original source