I have this code:
def closest():
citiescomp=[]
distances=[]
for i in range(0,len(city_names)-1):
citiescomp.append([city_names[i], citiesx[i], citiesy[i]])
while True:
try:
targetx=int(raw_input('Please enter the x coordinate of the city you wish to target: '))
if targetx in range(0,1000):
break
else:
print "Oops, it seems like you made a mistake. Try again by entering a number between 0 and 1000."
except ValueError:
print "Oops, it seems like you made a mistake. Try again by entering a number between 0 and 1000"
while True:
try:
targety=int(raw_input('Please enter the y coordinate of the city you wish to target: '))
if targety in range(0,1000):
break
else:
print "Oops, it seems like you made a mistake. Try again by entering a number between 0 and 1000."
except ValueError:
print "Oops, it seems like you made a mistake. Try again by entering a number between 0 and 1000"
for i in range(0, len(citiescomp)):
distance = (sqrt( (targetx - int(citiescomp[i][1]))**2 + (targety - int(citiescomp[i][2]))**2 ))
distance=round(distance, 2)
distances.append(distance)
shortest_distance=min(distances)
closest_city=distances.index(shortest_distance)
print "-"+citynames[closest_city+count],"at", round(shortest_distance,3),"units away"
citiescomp looks like this; citiescomp=[['city1', '111', '222'], ['city2', '333', '444'], ['city3', '555', '666'], ['city4', '777', '888']]
What happens is I find which city is the closest to the target city, but I want to sort them so that they are in order of shortest to farthest and print all distances while keeping the same name that was given to the coordinates of the city. "
Thanks,
Jaro
Editor's note:
You are mixing spaces and tabs in your indentations, a very bad habit that will make your code unusable for other people. Please use spaces only, the common standard is 4 spaces.