-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathdistance_calculator.py
44 lines (39 loc) · 1.5 KB
/
distance_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Uses the pythagorean theorem to calculate the distance between two points on a 2D plane.
# The points are represented as tuples of two numbers.
# The function should return a float.
# Sample :- startX = 0, startY = 0, endX = 3, endY = 4.
# So, according to pythagorean theorem, the distance between these two points is 5.0.
# Hope this helps!
import math
while True:
que=input("Do you want to calculate the distance between two points? (yes/no): ")
if que.lower() == "yes":
pass
elif que.lower() == "no":
print("Thank you for using the distance calculator!")
quit()
else:
print("Invalid input! Please enter 'yes' or 'no'.")
continue
startX = input("Enter starting x-coordinate: ")
if startX.isalpha():
print("Error! Input has to be a number!")
continue
startY = input("Enter starting y-coordinate: ")
if startY.isalpha():
print("Error! Input has to be a number!")
continue
endX = input("Enter ending x-coordinate: ")
if endX.isalpha():
print("Error! Input has to be a number!")
continue
endY = input("Enter ending y-coordinate: ")
if endY.isalpha():
print("Error! Input has to be a number!")
continue
startX = int(startX)
startY = int(startY)
endX = int(endX)
endY = int(endY)
distance = math.sqrt(math.pow(startX - endX, 2) + math.pow(startY - endY, 2))
print(f"The distance between ({startX}, {startY}) and ({endX}, {endY}) is {distance}.")