Reputation: 69
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
Upvotes: 5
Views: 49195
Reputation: 1131
The mistake is that when we assign a (number) to x, this is what is actually happening,
>>> x = (50)
>>> x
50
So if you want to create an empty list , you can do it this way
rows = 5
cols = 3
matrix = [[0 for c in range(cols)] for r in range(rows)]
Its gonna create something like this one,
>>> matrix
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
Upvotes: 1
Reputation: 1121226
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)]
Upvotes: 5
Reputation: 104682
There are a couple of issues in your code.
The most immediate one is that your Temp
value is not a sequence that you can assign things to, just an integer (the parentheses don't do anything). While you could make it into a tuple by adding a comma, that still won't let you assign values into Temp
after it has been created (tuples are immutable). Instead, you probably want a list.
However, there's another issue with your loop. A for loop like for value in sequence
assigns values from the sequence
to the variable value
. It doesn't assign indexes! If you need indexes, you can either use enumerate
or use a different looping construct, such as a list comprehension.
Here's a minimally changed version of your code that first creates a list of lenp
zeros, then replaces their values with the computed ones:
p=range(0,1200,10)
lenp=len(p)
rp=1.331
po=1000
T=280
Temp=[0]*lenp
for i, p_value in enumerate(p):
Temp[i]=T*(p_value/po)**rp
print T
Here's a more pythonic version that uses a list comprehension instead of an ordinary loop:
rp=1.331
po=1000
T=280
Temp = [T*(p_value/po)**rp for p_value in range(0,1200,10)]
Upvotes: 3
Reputation: 129477
Your Temp
is not a tuple, it's just an integer in parenthesis. You can fix this with a simple ,
. Consider the following:
>>> x = (42) # not a tuple
>>> x[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'
>>>
>>> x = (42,) # tuple
>>> x[0]
42
However, tuples are immutable, and you cannot perform such an assignment:
>>> x[0] = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
You can fix this issue by using a list instead.
Upvotes: 1