list assignment index out of range

3

A 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 the line corresponding to Tsup[0]=Tsup_i and I do not know how to solve ...

The Tsup I put before the cycle for as an empty list Tsup=[] , and what I want is that the 1st element of the list is equal to Tsup_i , that gives to determine P[i] e later in the code you can determine Tsup[i+1] using P[i] .

    
asked by anonymous 15.02.2016 / 11:51

1 answer

2

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

Tsup.append(Tsup_i)

But be aware that within for it will add a new element to each iteration, creating Tsup[0] , Tsup[1] , .... to Tsup[r] . If you want to associate only the first element you can do it outside of your for cycle.

    
15.02.2016 / 12:27