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]
.
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)))"
– Sofia Raimundo
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 theP
, thus:P.append()
. Done this is quite possible to fix syntax errors, hence has to see if the result is as expected.– Math
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
– Sofia Raimundo
Ah yes, now I see, you’re doing
Tsup[i]
and you end up trying to access a position ofTsup
that does not exist, maybe it was the case of you dorange(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.– Math
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
– Sofia Raimundo
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.
– Math