-2

For an assigment, we have to do a travelling salesman sort of code using geolocator code from a list of cities that is not given to us, assuming the first city in the list is the starting/ending city.

My code is incomplete, but barely, as I think all I have left to do is take the distance from the second to last to the initial city.

Anyways, I keep running into a "list index out of range" problem when trying to take the distance between two cities in the list.

I think I've made an if/else statement for everything that could push my indices out of range, but I still get the error. I'm reasonably new to Python, so please excuse my poor structure/syntax. The imports I have are mandatory for this project, so again, please don't judge. The mistake may be really stupid and obvious, but if it is, I'm having trouble seeing it.

#!/usr/bin/env python
cityfile = open(sys.argv[1], "r")
citylistwithn = cityfile.readlines()
citylist = list(map(lambda each:each.strip('\n'), citylistwithn))
initcity = 0

def main():
    p = 0
    distancebetweencities = 1
    totaldistance = 0
    cityat = 0
    while len(citylist) >= 1:
        currentdistance = vincenty(citycoords(citylist[cityat]), citycoords(citylist[p])).miles
        if currentdistance > distancebetweencities:
            distancebetweencities = currentdistance
            if int(p) < len(citylist)-1:
                p += 1
            else:
                p = p
        elif currentdistance < distancebetweencities:
            distancebetweencities = distancebetweencities
            if int(p) < len(citylist)-1:
                p += 1
            else:
                p = p
        elif currentdistance == distancebetweencities:
            totaldistance += distancebetweencities
            citylist.pop(int(cityat))
            if int(p) >= 1:
                cityat = int(p) - 1
            else:
                cityat = int(p)
    print(totaldistance)
main()

Please help me stackoverflow, you're my only hope

EDIT: Traceback, and cut out some unnecessary code:

Traceback (most recent call last):
  File "/home/user/assignment6.py", line 43, in <module>
    main()
  File "/home/user/assignment6.py", line 22, in main
    currentdistance = vincenty(citycoords(citylist[cityat]), citycoords(citylist[p])).miles
IndexError: list index out of range
Wakeen
  • 3
  • 2
  • Please cut your code down to a [minimal example](http://stackoverflow.com/help/mcve) and provide the full error traceback. – jonrsharpe Oct 07 '14 at 20:48
  • Please tell us (by running in the debugger, or just adding a bunch of `print` calls) what's in `cityat`, `p`, and `citylist`, and verify that it's one of the two immediate `[]` expressions in this code and not one of the functions you call that's raising the exception. – abarnert Oct 07 '14 at 21:14
  • Meanwhile, you're doing `int(p)` all over the place. What type is `p`? It looks like it ought to be an `int` in the first place, meaning those calls are unnecessary. But if they _are_ necessary, then the problem most likely has to do with whatever type `p` is, maybe its `__index__` method or something. – abarnert Oct 07 '14 at 21:15
  • Also, you've got lines like `distancebetweencities = distancebetweencities` and `p = p` and so on all over the place, that can't possibly do anything. What are they for? If it's not clear what your code is supposed to be doing, it's hard to fix it to do what it's supposed to do… – abarnert Oct 07 '14 at 21:16

1 Answers1

0

I'm not sure how this is supposed to be working, but here's something pretty clear:

  • You keep calling citylist.pop(), and never add anything to it, so it's always getting smaller.
  • You keep doing p += 1, and never reduce it or reset it to any other value, so it's always getting larger.
  • You don't exit until citylist is down to 1 city.
  • Every time through the loop, you do some expression involving citylist[p].

So, assuming p ever gets to 2 or higher (which it seems like it usually will), you're guaranteed to eventually hit a point where citylist has less than p elements, so the next citylist[p] will raise an IndexError.

Most likely the problem is that you're supposed to be reducing or resetting p somewhere, but never are. If I had to guess, I'd guess that when you citylist.pop(int(cityat)), if p >= cityat you need a p -= 1. But since I can't understand what your code is supposed to be doing, I can't really be sure of that.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Even though you didn't understand my code, you seemed to understand the error. Fixed and working, that p >= cityat was what it needed – Wakeen Oct 07 '14 at 21:38