Python Forum
"List index out of range" for output values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"List index out of range" for output values
#1


Hi! I'm attempting to take calculated values from a loop and output them into a text file. I currently have:
fout = open("Wishbone 1N-24HZ Output.txt", "w")
while p!=W:
    fout.write("{:f} {:f} {:f} {:f} \n".format(NewTVD[i], NewN[i], NewE[i], DLS[i]))
    p=p+1
fout.close()
W is a count of the number of values in a list that is read from an input text file.

I have the variables defined as lists and calculates values being put in:
DLS=[]
dls =...
    DLS.append(dls)
I keep getting an error of "list index out of range" and I'm not sure what that means or how to fix it. I'm using Spyder, and the DLS list does not show up in the variable explorer when I run through my loop to calculate that. I'm not sure if that is a clue to what is going wrong. I'm fairly new to coding in Python.
Reply
#2
It's really hard to help without enough information to reproduce the problem. Try hard-coding (no files, input() calls or anything like that) anything right before the problem-code, and then post here the smallest necessary to cause it. It should be no more than 5-10 lines if you've culled well.
Reply
#3
I have tried to just do print "variable name" and all the code before the output loop works to correctly calculate values. So I'm not sure what you mean by hard coding that snippet that I have posted. That is my problem code. The rest of my code looks like:
import math

md=[]
"""Measured depth, ft"""
azi=[]
"""Azimuth angle angle, degrees"""
inc=[]
"""Inclination angle, degrees"""
NewTVD=[]
NewE=[]
NewN=[]
dls=[]

fin = open("Wishbone 1N-24HZ.txt", "r") # open file for reading
for line in fin:
    mds, incs, azis = line.split()
    md += [float(mds)]
    inc += [float(incs)]
    azi += [float(azis)]
fin.close()
 
Pi = 3.14159
"""Assigns value to pi, unitless"""
i=1
p=1
md[0]=0
azi[0]=0
inc[0]=0
W=len(md)

while i!=W:
    ChangeMD = md[i]-md[i-1]
    Depart1to2 = ChangeMD * math.cos(azi[i] * (Pi / 180))
    ChangeN1to2 = Depart1to2 * math.cos(azi[i] * (Pi / 180))
    ChangeE1to2 = Depart1to2 * math.sin(azi[i] * (Pi / 180))
    I1 = inc[i] * (Pi / 180)
    I2 = inc[i-1] * (Pi / 180)
    A1=azi[i]*(Pi/180)
    A2=azi[i-1]*(Pi/180)
    N=md[i]*math.sin(I2)*math.cos(A2)
    E=md[i]*math.sin(I2)*math.sin(A2)
    TVD=md[i]*math.cos(I2)
    ChangeTVD1to2= ChangeMD* math.cos(inc[i] * (Pi / 180))
    tvd = TVD + ChangeTVD1to2
    NewTVD.append(tvd)
    n= N + ChangeN1to2
    NewN.append(n)
    e = E + ChangeE1to2
    NewE.append(e)
    dogleg =(((inc[i] - inc[i-1]) / ChangeMD) ** 2 + (math.sin(I1 + I2) / 2)* (((azi[i] - azi[i-1]) / ChangeMD) ** 2) ** (1 / 2)) * 100
    dls.append(dogleg)
    i=i+1
Reply
#4
You should upload the file: Wishbone 1N-24HZ.txt
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove extra space from output list? longmen 3 1,760 May-05-2022, 11:04 PM
Last Post: longmen
  How to fix list index out of range longmen 26 5,695 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,289 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  Index List a04j 2 2,881 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  List index out of range when turning CSV into dict ranbarr 15 6,282 May-12-2021, 10:38 AM
Last Post: ranbarr
  List vs index: Frederico_Caldas 5 3,525 Jul-03-2020, 10:55 AM
Last Post: DeaD_EyE
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,204 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  How can I run a function inside a loop every 24 values of the loop iteration range? mcva 1 2,101 Sep-18-2019, 04:50 PM
Last Post: buran
  list index out of range mcgrim 2 2,859 May-25-2019, 07:44 PM
Last Post: mcgrim
  dictionaries and list as values Pippi 6 3,423 Apr-13-2019, 09:05 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020