Reputation: 61
I am getting
Failed example:
distances('Raleigh', 'Centreville', cities)
Exception raised:
Traceback (most recent call last):
File "C:\Users\agsmi\anaconda3\lib\doctest.py", line 1336, in __run
exec(compile(example.source, filename, "single",
File "<doctest __main__.distances[0]>", line 1, in <module>
distances('Raleigh', 'Centreville', cities)
File "C:\Users\agsmi\Desktop\st114\homework5.py", line 110, in distances
return (6371 * (math.acos(math.sin(cities[city1[0]]) * math.sin(cities[city2[0]]) + math.cos(cities[city1[0]]))) * math.cos(cities[city2[0]]) * math.cos(cities[city1[1]] - cities[city2[1]])), ((math.sqrt(cities[city2[0]] - cities[city1[0]]**2 + cities[city2[1]] - cities[city1[1]]))**2)
KeyError: 'R'
**********************************************************************
1 items had failures:
1 of 2 in __main__.distances
***Test Failed*** 1 failures.
From this code
def distances(city1:str, city2:str, cities:dict):
"""
Description: Takes in two strings (city1 and city2), searches to see if those strings are found in the cities dictionary. If they are, this calculates the great circle distance and the Euclidean distance between the cities.
>>> distances('Raleigh', 'Centreville', cities)
***
>>> distances('NYC', 'Tianjin', cities)
(nan, nan)
"""
cities = {'Raleigh':(35.7796, -78.6382), 'Tianjin':(39.129498, 117.251038), 'Centreville':(32.9446, -87.1386), 'Los Angeles':(34.0522, -118.2437), 'Houston':(29.749907, -95.358421)}
import math
if city1 in cities and city2 in cities:
return (6371 * (math.acos(math.sin(cities[city1[0]]) * math.sin(cities[city2[0]]) + math.cos(cities[city1[0]]))) * math.cos(cities[city2[0]]) * math.cos(cities[city1[1]] - cities[city2[1]])), ((math.sqrt(cities[city2[0]] - cities[city1[0]]**2 + cities[city2[1]] - cities[city1[1]]))**2)
else:
return(math.nan, math.nan)
I don't know what I am doing wrong. Other stuff is probably wrong with the code too, but I am confused about this right now. I am a beginner and this is for an assignment that asks for:
Following the function design recipe, define a function distances that takes in two strings city1 and city2 and a dictionary cities as parameters. The dictionary cities has city names as keys and tuples of (latitude, longitude) as their values. The function returns a tuple of two items. (You do not need to include examples in your docstring.)
• If city1 and city2 are in the dictionary cities then the first item in the returned tuple is the great-circle distance between city1 and city2 and the second item in the returned tuple is the Euclidean distance between city1 and city2.
I don't know why I am getting this error.
Upvotes: 0
Views: 3453
Reputation: 71
You're getting R from first letter of from parameter if you put "Houston" instead of then you will get H so it show indexing error please find out by subtract source city key to destination key or multiply as per your mathematical way.
Upvotes: 0
Reputation: 369
Your mistake is that you take the first character from the string, and not the first value from the tuple:
You have so:
cities[city1[0]] - > 'Raleight'[0] - > 'R' (key: 'R')
Try to do this:
cities[city1][0]
And do this with everyone else.
Upvotes: 4