Question: NOTE : MODIFY THIS PYTHON CODE SO THAT IT WORKS FOR BADLY FORMATED MAP REFERENCES, your program should exit, reporting the first bad map reference.
NOTE : MODIFY THIS PYTHON CODE SO THAT IT WORKS FOR BADLY FORMATED MAP REFERENCES, your program should exit, reporting the first bad map reference.
For example:
Enter trip map references: D2 F23 Ja E23 Z2
Bad reference:
# program that returns total distance def distance(reference): # split reference in different points points = reference.split() # initialize dist = 0 dist = 0 # traverse from Point-2 index to last-point index for i in range(1, len(points)): # x1 = x value of point (i-1) x1 = points[i - 1][0] # y1 = y value of point (i-1) y1 = int(points[i - 1][1:]) # x2 = x value of point i x2 = points[i][0] # y2 = y value of point i y2 = int(points[i][1:]) # if point (i-1) is wrong representation if (x1 < 'A' or x1 > 'Z') or (y1 < 1 or y1 > 26): print('Bad reference: ' + points[i - 1]) exit() # if point i is wrong representation if (x2 < 'A' or x2 > 'Z') or (y2 < 1 or y2 > 26): print('Bad reference: ' + points[i]) exit() # calculate change in x value from Point (i-1) to Point (i) xchange = (ord(x1) - ord(x2)) * 0.5 # calculate change in y value from Point (i-1) to Point (i) ychange = (y1 - y2) * 0.5 # using Pythagorous theorem dist += (xchange ** 2 + ychange ** 2) ** 0.5 # return final distance return dist # asking reference from user reference = input("Enter trip map references: ") # print Total Distance print('Total distance = ' + "{:.1f}".format(distance(reference)) + ' 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
