2

I am receiving "list index out of range". I think I'm naming one of the variables incorrectly. It might make more sense if i add the rest in then.

I am trying to print out the differences in coordinates in a list. I am certain it screws up on str(distance_list[i]).

Relevant code:

# Get the maximum distance from the user
maxDistance = float(raw_input("What is the maximum distance from the base?"))

# Set the base N,E values
#baseEasting = float(raw_input("What is the easting of the base?"))
#baseNorthing = float(raw_input("What is the northing of the base?"))
baseEasting = "346607"
baseNorthing="6274191"

#TODO: Place the values for meterological stations into the lists
stationCoords = [ [476050, 7709929],[473971,7707713],[465676,7691097] ,[515612,7702192] ,[516655,7704405],[519788,7713255],[538466,7683341] ]
numCoords = len(stationCoords)

distance_list = []
for i in range (0, numCoords):
    stationNorthing=stationCoords[i][0]
    stationEasting=stationCoords[i][1]
    distance = calculateDistance(stationNorthing, stationEasting, EASTING_BASE, NORTHING_BASE)
    if distance <= maxDistance:
# Calculate output string
        strTextOut = "Co-ordinates: " + str(distance_list[i])
        + ", at: " + str(round(distance, 0)) + " m"
        # Output the string
        print(strTextOut)

Hopefully that's all that is relevant. But the stationCoords already has values in it.

1
  • You are receiving "list index out of range". Fine. But at which line do you get it? The back trace is a wonderful thing. Make use of it by telling us about it.
    – glglgl
    May 23, 2013 at 14:48

2 Answers 2

5

distance_list is an empty list (because of the line distance_list = []), and you are attempting to read values from it with distance_list[i]. This is guaranteed to fail because the list is empty, so no index would be valid.

Maybe you meant to type stationCoords[i] instead? That makes more sense because you are attempting to print the coordinates there.

5
  • I think it worked! now the next line is saying "bad operand type for unary +: 'str'" May 23, 2013 at 15:06
  • I'll search python again and see what the hell that means lulz May 23, 2013 at 15:07
  • 1
    @AndrewMcVicar It means that you shouldn't have separated the line starting with strTextOut = into two lines. If you do want to separate it into two lines, put \ at the end of the first.
    – interjay
    May 23, 2013 at 15:09
  • It works now! thankyou guys. I owe you my life! I would rate but i'm not on a high enough level, but thankyou. thankyou thankyou thankyou May 23, 2013 at 15:16
  • @AndrewMcVicar No problem. Be sure to accept the answer which solved your question by pressing the checkmark on the left.
    – interjay
    May 23, 2013 at 15:18
2

You probably want to add a:

distance_list.append(distance)

just before your if statement, or similar, or just replace:

str(distance_list[i])

with

str(distance)

and get rid of distance_list entirely, if you don't need to access the distances later.

Not the answer you're looking for? Browse other questions tagged or ask your own question.