Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON LANGUAGE QUESTION, CREATE 2 FUNCTIONS IN PYTHON TO CALL IN MAIN: supplements: The file cities.info (see below) contains city names, countries, and the distance

PYTHON LANGUAGE QUESTION, CREATE 2 FUNCTIONS IN PYTHON TO CALL IN MAIN:  supplements: The file "cities.info" (see below) contains city names, countries, and the distance in Km from the city of Toronto. The file is in the following format: CITY:COUNTRY:DISTANCE where each field is separated from other fields by the ':' character. The file format will always be the same for all records, but the file itself many contain ANY number of records. file: cities.info ========================================== Toronto:Canada:0.000 Melbourne:Australia:16267.128 San Francisco:United States:3643.327 Tokyo:Japan:10344.956 Paris:France:6000.842 London:United Kingdom:5713.099 Stuttgart:Germany:6378.285 Vancouver:Canada:3358.600 Vienna:Austria:6917.006 New York:United States:550.272 Rome:Italy:7080.692 Shanghai:China:11423.028
 PART A:  Write the code for a Python function with the following prototype: def loadDataIntoDictionary(fn) :  This function accepts the name of a file as a parameter 'fn' and loads the data in the file into a dictionary where the city is the key and the country and distance are stored as values. How the values are stored for each key will be up to you! This function displays the number of records loaded from the file as: n city records loaded... (where n is the number of records in the file) and then returns the loaded dictionary. If the file is not found, then the error message: ERROR... file data could not be loaded is displayed and the program terminates! NOTE: You MUST use Python exception handling to manage this error and program functionality. The correct file WILL BE available on the server when submitting your solution.
PART B:  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. 
MAIN PROGRAM: # Your solution may ONLY use the python modules listed below 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions