list index out of range

Asked

Viewed 23,293 times

1

Tsup=[]
Te=0
dt=1
t=range(0,3600*2,dt)
r=len(t)
for i in range(0,r):
   Tsup.append(20)
....
   Tsup.append( (((Pj[i]+Ps-Pc[i]-Pr[i])*(dt))/((m_al*c_al[i])+(m_a*c_a[i])))+Tsup[i] )

   if i==r: 
      Te=(((Pj[i]+Ps-Pc[i]-Pr[i])*(dt))/((m_al*c_al[i])+(m_a*c_a[i])))+Tsup[i]

variacao=[]
T_est=0
for i in range(0,l+1):
    variacao.append(abs(Te-Tsup[i]))
    if variacao[i] < 0.1 and variacao[i-1] > 0.1:
       T_est=Tsup[i]

The error "list index out of range" appears in the variation line.append(abs(Te-Tsup[i])).

  • 1

    I think the Tsup.append(20) is unnecessary, you are always putting a value of 20 in the Tsup every iteration of the first for. You could use your own Tsup_i that is 20, when you want to use this value. Already about the error out of range. You have to make sure that Tsup contains at least l+1 items.

  • 1

    You are trying to access an index that does not exist in the list. For example access element 4 of a list with 3 elements.

  • Tsup.append(20) was to define that the 1st element of the Tsup list was 20, so it should be defined for the for cycle, right?

  • Where is the variable l? The error is not in the append, but in the Tsup[i], as pointed out by @Math. Sometime i is bigger than the list size because it goes from zero to l+1, then I assume your list Tsup is smaller than l+1.

  • It was a mistake and I already edited, the variable l was r. I tried to comment on the last part and print after "Tsup.append( (((Pj[i]+Ps-Pc[i]-Pr[i])(dt)/((m_alc_al[i])+(m_a*c_a[i]))+Tsup[i] )" to see if you noticed what Tsup values and the error "list index out of range" now appears in a line of code that I don’t have there and that is higher, that is, the error comes from behind..

  • Okay, we found the bug, your list Tsup has r items and you try to access one beyond the last, with the for that goes up to r+1, move to r instead of r+1 should solve this problem of out of range, I just don’t know if the logic is right.

  • Yeah, that makes sense. I’ve already changed the two to for i in range(0,r): but it still gives the same error "list index out of range" in the variacao.append(abs(Te-Tsup[i])).

  • Tsup is a certain vector? if it is smaller than len(r)that mistake is going to happen anyway

  • It’s a list. So how do I fix this?

  • humm I was looking calmly if Pj, Ps, Pcor Pr are less than len(r) will happen the error mentioning, what is the size of these lists pj,ps,pc and pr ?? may not be smaller that 7200 which is your r

  • Inside the 1st is before calculating Tsup, it is calculated: Pj.append(((A)*2) * b * (1 + a * (Tsup[i] - (20+K)))) Ps = a_S * S * D Pc.append(Math.pila_f[i](Tsup[i]- Ta)Nu[i]) Pr.append(Math.piDandbo(((Tsup[i])4)-(Ta4)). as only depend on i, will have the size of r, right?

  • yes everyone has to be greater or equal than r all your lists you have several Pr, Pj, Pc, c_a e c_al check the size of all of them has to be bigger, I ran your code here and created these lists randomly your code ran without index error, have other errors like var lthat doesn’t exist.

  • but simply doing this: dt=1 t=range(0,3600*2,dt) r=Len(t) Tsup=[] Pj=[] Tsup.append(Tsup_i) for i in range(0,r): Pj.append(((A)**2) * R20 * (1 + a * (Tsup[i] - (20+K)))) print Pj immediately gives error list index out of range in line Pj.append(((((A)**2) * R20 * (1 + a * (Tsup[i] - (20+K))), where A, R20, a and K are defined at the beginning. How is it possible?

  • @Sofiaraimundo it is obvious that will give error Tsup you started with empty size it is not the size of r

Show 9 more comments

1 answer

1

First you did a for r items to complete the variable TSup. Then you did a for r + 1 elements to access Tsup[i]. It’s natural to make a mistake list index out of range. The problem is there. I haven’t seen your logic, but you need to make a loop of r + 1 elements? If not, the solution is just to put for i in range(0,r): instead of for i in range(0,r+1):, how it was placed.

EDIT:

There is a syntax error in the first for. You did not put the two points : at the end of it.

One care you have to take is about identation, okay? The second one is indented needlessly. The interpreter will complain about that.

  • Yeah, that makes sense. I’ve already changed the two to for i in range(0,r): but it still gives the same error "list index out of range" in the variacao.append(abs(Te-Tsup[i])).

  • Copy your code and run it on my computer and gave a syntax error. With that, I edited my answer. Have a look.

  • Thanks for the repair, but it was just a mistake to pass here, in my code are the two points.

  • As I already mentioned, I initialized Tsup in 20, that is, I made the 1st element of the Tsup list 20. By doing this: Nu_c_n.append(A2[i]*(G[i]*Pra[i])**m2[i]), with A2, G, Pra and m2 initialized in the form A2=[] and without assigning an initial value like Tsup, can be the origin of the error?

  • I don’t know. Your code worked on my computer. I had to comment on the line where the variable was referenced Pj (I believe you initialize it somewhere in the code not shown). And everything works. Make sure you are using the correct file.

  • I really don’t understand what’s going on. I am trying to do it in parts, to see if I can find the error more easily and I just did so now: dt=1 t=range(0,3600*2,dt) r=Len(t) Tsup=[] Pj=[] Tsup.append(Tsup_i) for i in range(0,r): Pj.append((((A)**2) * R20 * (1 + a * (Tsup[i] - (20+K))) print Pj and immediately gives error list index out of range in the row Pj.append((((A)**2) * R20 * (1 + a * (Tsup[i] - (20+K))), where A, R20, a and K are set at the beginning.

  • Could you post the full code? I would like to test it on my computer, but there are some variables that are not defined in the code snippet you posted.

  • If you are going to post the code in full, I like to use this site http://pastebin.com/

  • I put on this site right now the complete code.

  • Give me the URL so I can see it, then?

  • http://pastebin.com/BNBM8tDM

  • The code you gave me is different from the code of the question you created. In the code you gave me, the variable Tsup has only 1 element (it executes only one append()), but you travel r elements.

Show 7 more comments

Browser other questions tagged

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