Skip to content

Commit 3b42328

Browse files
Add files via upload
1 parent 4b2d767 commit 3b42328

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

distance_calculator.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Uses the pythagorean theorem to calculate the distance between two points on a 2D plane.
2+
# The points are represented as tuples of two numbers.
3+
# The function should return a float.
4+
5+
# Sample :- startX = 0, startY = 0, endX = 3, endY = 4.
6+
# So, according to pythagorean theorem, the distance between these two points is 5.0.
7+
8+
# Hope this helps!
9+
10+
# Importing module(s)
11+
import math
12+
13+
# Main loop
14+
while True:
15+
use_program=input("Do you want to calculate the distance between two points? (yes/no): ")
16+
if use_program.lower() == "yes":
17+
pass
18+
elif use_program.lower() == "no":
19+
print("Thank you for using the distance calculator!")
20+
quit()
21+
else:
22+
print("Invalid input! Please enter 'yes' or 'no'.")
23+
continue
24+
25+
# Input validation for startX, startY, endX, endY
26+
27+
# startX
28+
while True:
29+
startX = input("Enter starting x-coordinate: ")
30+
if not startX.isnumeric():
31+
print("Error! Input has to be a number!")
32+
continue
33+
else:
34+
break
35+
36+
# startY
37+
while True:
38+
startY = input("Enter starting y-coordinate: ")
39+
if not startY.isnumeric():
40+
print("Error! Input has to be a number!")
41+
continue
42+
else:
43+
break
44+
45+
# endX
46+
while True:
47+
endX = input("Enter ending x-coordinate: ")
48+
if not endX.isnumeric():
49+
print("Error! Input has to be a number!")
50+
continue
51+
else:
52+
break
53+
54+
# endY
55+
while True:
56+
endY = input("Enter ending y-coordinate: ")
57+
if not endY.isnumeric():
58+
print("Error! Input has to be a number!")
59+
continue
60+
else:
61+
break
62+
63+
# Converting the values to floats
64+
startX = float(startX)
65+
startY = float(startY)
66+
endX = float(endX)
67+
endY = float(endY)
68+
69+
# The calculation
70+
distance = math.sqrt(math.pow(startX - endX, 2) + math.pow(startY - endY, 2))
71+
print(f"The distance between ({startX}, {startY}) and ({endX}, {endY}) is {distance} units.")

0 commit comments

Comments
 (0)