The coordinates of different persons are given in the following dictionary.
position = ('A': (2, 5), 'B': (1, 7), 'C': (9, 5), 'D': (2, 6))
You will take 2 inputs for person X's location where the first
input specifies person X's current location in the x-axis and the second input specifies person X's current location in the y-axis.
Your work is to find the person closest to person X by
calculating the
distance between person X and every other person in the dictionary.
Hint: The distance between two coordinates is given by the
formula (You
need to import math):
math.sqrt((x2-x1)**2 + (y2-y1)**2)
Sample Input 1:
Enter person X's x coordinate: 3
Enter person X's y coordinate: 6
Sample Output 1:
D
position = {'A': (2, 5), 'B': (1, 7), 'C': (9, 5), 'D': (2, 6)}
x = int(input("Enter person X's x coordinate: "))
y = int(input("Enter person X's y coordinate: "))
min_dist = None
point_name = ''
for point in position:
x1 = position[point][0]
y1 = position[point][1]
dist = (x-x1)**2 + (y-y1)**2
if min_dist is not None:
if min_dist > dist:
min_dist = dist
point_name = point
else:
min_dist = dist
point_name = point
print(point_name)
Comments
Leave a comment