440,550 Members | 1,189 Online Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

# distance between multiple locations

 P: 6 so after that part, I have to make a function that returns a list of total distances between each of the cities in the cities list (a list of pair-lists) and I have to have it so the function calculates the total distances between each city and all the other cities. The index of the results list should correspond to the index in the cities list so that index i in the results list represents the total distance between cities[i] and all the other cities. I understand that it's telling me what to do but don't understand how to write it out :( right now I have this as the function to calaculate the total distance but it seems as if i'm missing something.. Expand|Select|Wrap|Line Numbers   def total_distances(cities):     distance = 0.0     for i in cities():         distance = distance + get_distance(x1,y1,x2,y2)     return distance   and this is my previous function that I used in the total_distance function that calculates the distance between two cities Expand|Select|Wrap|Line Numbers def get_distance(x1,y1,x2,y2):      return sqrt(((x2-x1)**2) + ((y2-y1)**2))   Nov 19 '08 #1
3 Replies

 Expert 100+ P: 469 I think you're going to need nested for loops. Here's my idea for the pseudocode: Expand|Select|Wrap|Line Numbers make a list of distances for each city:     set a distance variable to zero     for each other city:         add the distance between the cities to distance     put distance in the list   You won't need to worry about adding the distance between a city and the same city, because, being zero, it won't affect the sum. Nov 19 '08 #2

 P: 6 @boxfish ok.so now i have it like this but I how do i add the distance between the citeis to the distance ? which is this part : Expand|Select|Wrap|Line Numbers  distance += get_distance(i,i,n,n)   Expand|Select|Wrap|Line Numbers def total_distance(cities):     total = []     for i in cities:         distance = 0         for n in cities:             distance += get_distance(i,i,n,n)             total.append(distance)     return distance   Nov 25 '08 #3

 Expert 100+ P: 469 Expand|Select|Wrap|Line Numbers def total_distance(cities):     total = []     for i in cities:         distance = 0         for n in cities:             distance += get_distance(i,i,n,n)             total.append(distance)     return distance   I don't see a problem with how you find the distance between the two cities. The thing that seems wrong is that the line where you append distance to total is inside of the second for loop. That means that every time you add a distance to your distance variable, you append the current tally to total. If I understand your assignment correctly, you're only supposed to append the total distance to total, so you want: Expand|Select|Wrap|Line Numbers def total_distance(cities):     total = []     for i in cities:         distance = 0         for n in cities:             distance += get_distance(i,i,n,n)         total.append(distance)     return distance   I hope I have this right and that it's helpful. Good luck. Nov 25 '08 #4 