I am a beginner at Python, and will appreciate some explanation on the faults in my attempts. I'd also appreciate some help on how to proceed from here. I'll link the challenging statements first, and the verbose code block after.
I have defined a dictionary: loc_dictionary
to receive key-value pairs.
The key is a user-inputted name and the value is a list of two values to represent latitude and longitude, also inputted by the user.
I am trying to find the difference between the latitude in two dictionary keys in a loop:
deltalat = loc_dictionary[i + 1][0] - loc_dictionary[i][0]
print(deltalat)
The above code is meant to access the variable in the next iteration minus the variable in the current iteration of the dictionary. It tells me that I cannot concatenate the string to int ([i + 1]).
TypeError: can only concatenate str (not "int") to str
Then, I tried another method using nested for loops:
for i in range(0, len(loc_dictionary)):
for j in range(1, len(loc_dictionary)):
deltalat = loc_dictionary[j][0] - loc_dictionary[i][0]
print(deltalat)
This throws the following error:
deltalat = loc_dictionary[j][0] - loc_dictionary[i][0]
KeyError: 1
Which I believe means that the dictionary key being referenced cannot be found. Using Jupyter, I got some more help on an error telling me dictionary keys do not support indexing. What should I do next? Kindly find the complete code below:
loc_dictionary = {}
# Method for accepting coordinates
def route():
count = 0
primary_loc_key = input('Please type in the name of first location.\n>>> ').strip()
primary_loc_value = [
eval(input('Please type in the latitude of \'' + primary_loc_key + '\'.\n>>> ').strip()),
eval(input('Please type in the longitude of \'' + primary_loc_key + '\'.\n>>> ').strip())]
loc_dictionary[primary_loc_key] = primary_loc_value
location_exit_loop = 'done'
loc_input_key = ''
loc_input_value = []
print('<<<Type \'Done\' as the location name when all locations have been inputted.>>>')
while loc_input_key.strip().lower() != location_exit_loop:
loc_input_key = input('Please type in the name of the ' + str(count + 1) + ' stop.\n>>> ').strip()
if loc_input_key.strip().lower() != location_exit_loop:
loc_input_value = [eval(input('Please type in the latitude of ' + loc_input_key + '.\n>>> ').strip()),
eval(input('Please type in the longitude of ' + loc_input_key + '.\n>>> ').strip())]
else:
loc_input_value = None
loc_dictionary[loc_input_key] = loc_input_value
count += 1
if (count - 1) < 2:
print('You have a single stop.')
else:
print('You have ', count - 1, 'stops to be calculated.')
del loc_dictionary['done']
print(loc_dictionary)
return loc_dictionary
# Calculate through loop
def coordinates():
latitude = 0
longitude = 0
# loop through items in the loc_dictionary
for i in loc_dictionary:
latitude = loc_dictionary[i][0]
longitude = loc_dictionary[i][1]
print(i + '\nLatitude: ' + str(latitude) + '\nLongitude: ' + str(longitude))
# for i in loc_dictionary:
# deltalat = loc_dictionary[i + 1][0] - loc_dictionary[i][0]
# print(deltalat)
for i in range(0, len(loc_dictionary)):
for j in range(1, len(loc_dictionary)):
deltalat = loc_dictionary[j][0] - loc_dictionary[i][0]
print(deltalat)
route()
coordinates()