Question: Write the code for a Python function with the following prototype: def computeDistance(table, cityFrom, cityTo) : This function accepts 3 parameters: 1. A dictionary of
Write the code for a Python function with the following prototype: def computeDistance(table, cityFrom, cityTo) : This function accepts 3 parameters: 1. A dictionary of cities, countries, and distances created from PART A named 'table', 2. The name of a city to compute the distance from 'cityFrom', 3. The name of a city to compute the distance to 'cityTo', and computes and displays the distance in km from 'cityFrom' to 'cityTo'. The case of the letters in the parameter for each city name should not matter. If either city name is NOT found in the 'table' dictionary, then an error message: ERROR... one or more city names NOT found! is displayed and the function ends. For example, if the dictionary in the file "cities.info" (above) were loaded into 'table', then the following function calls: computeDistance(table, "toronto", "stuttgart") # would display: distance from Toronto, Canada to Stuttgart, Germany is: 6378.285 km computeDistance(table, "London", "Zurich") # would display: ERROR... one or more city names NOT found! computeDistance(table, "pARis", "VIENNA") # would display: distance from Paris, France to Vienna, Austria is: 916.164 km (6917.006 - 6000.842) NOTE: The larger distance to the city of Toronto MUST first be computed in order to get the correct value for distance.
import math import random import string import collections import datetime import re import time import copy # YOUR CODE BELOW... def loadDataIntoDictionary(fn) : your code here... # end def def computeDistance(table, cityFrom, cityTo) : your code here... # end def def main( ) : table = loadDataIntoDictionary("cities.info") computeDistance(table, "toronto", "stuttgart") computeDistance(table, "London", "Zurich") computeDistance(table, "pARis", "VIENNA") computeDistance(table, "toronto", "TORONTO") computeDistance(table, "toronto", "Seoul") computeDistance(table, "tokyo", "San francisco") # end main( ) if __name__ == "__main__" : main( ) The OUTPUT should be EXACTLY as displayed below: 12 city records loaded... distance from Toronto, Canada to Stuttgart, Germany is: 6378.285 km ERROR... one or more city names NOT found! distance from Paris, France to Vienna, Austria is: 916.164 km distance from Toronto, Canada to Toronto, Canada is: 0.000 km ERROR... one or more city names NOT found! distance from Tokyo, Japan to San Francisco, United States is: 6701.629 km Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
