assignment list index out of range

Asked

Viewed 13,184 times

4

Part of my Python program is:

dt=1
maxt=3600*2
t=range(0,maxt,dt)
r=len(t)
Tsup_i=20

for i in range(1,r):
    Tsup[0]=Tsup_i
    P[i]=((I_dc)**2) * R20 * (1 + a * (Tsup[i] - (20+K)))

And the error is appearing

"Indexerror: list assignment index out of range"

In row corresponding to Tsup[0]=Tsup_i and I don’t know how to solve...

The Tsup I put before the cycle for as an empty list Tsup=[], and what I want is for the first element of the list to be equal to Tsup_i, that can be determined P[i] and further down the code can determine Tsup[i+1] using P[i].

1 answer

4


Tsup is an empty list and you try to access your first element by doing Tsup[0]. To associate a new element to it the correct one is to use the function append():

Tsup.append(Tsup_i)

But note that inside the for it will add a new element for each iteration, creating Tsup[0], Tsup[1], .... until Tsup[r]. If you want to associate only to the first element you can do this out of your cycle for.

  • ok, fiz isso, ficaria então:

dt=1
maxt=3600*2
t=range(0,maxt,dt)
r=len(t)
Tsup_i=20
Tsup=[]
P=[]
 for i in range(1,r): 
 Tsup.append(Tsup_inicial)

 P[i]=((I_dc)**2) * R20 * (1 + a * (Tsup[i] - (20+K))) P.append(P[i]) but then gives the error: "Indexerror: list index out of range" in "P[i]=((I_dc)**2) * R20 * (1 + a * (Tsup[i] - (20+K)))"

  • I can’t be sure of the logical part, but of the syntax part the error in Tsup is fixed yes, now you have to do the same for the P, thus: P.append(). Done this is quite possible to fix syntax errors, hence has to see if the result is as expected.

  • I did that and it still gives error: P.append(((I_dc)**2) * R20 * (1 + a * (Tsup[i] - (20+K))) "Indexerror: list index out of range" on this line , same line

  • Ah yes, now I see, you’re doing Tsup[i] and you end up trying to access a position of Tsup that does not exist, maybe it was the case of you do range(0, r). If this doesn’t solve I think you’ll have to post more parts of your code so I can understand what you’re trying to do and help refactor your code.

  • Thank you very much! Apparently this problem is solved. Now another has come to me. After the previous code, I have this part: if i==r: Te=((((P[i]+Ps-Pc[i]-Pr[i])(dt)/((m_alc_al[i])+(m_a*c_a[i]))+Tsup[i] , and gives me an error that says: "Nameerror: name 'Te' is not defined". Where the variables: P[i], Ps, Pc[i], Pr[i], m_al, c_al[i] are calculated previously without problem. What I mean is that Te is the value corresponding to the last position of the list, that is, when i=r. And since Te is supposed to be a single value and not a list of values, I didn’t think I had to start it before

  • I could not understand very well the problem, even because in the area of comments it is difficult to visualize. I suggest you create a new question explaining this problem even to maintain the organization of the site, separating one question per topic. And if that answer answered your question I suggest you consider marking it as accepted by clicking on the check mark (V) next to it.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.