Skip to content

Distance Calculator #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Distance Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Program
This is a *distance calculator*, used to calculate the distance between two points on a 2D plane.

# How to use

## Use any of the following commands -
`python distance_calculator.py`

`python3 distance_calculator.py`

## Example
![image](https://github.com/user-attachments/assets/987537c2-236e-4560-9a06-3de4bbe98f5e)

Made by Mathdallas_code(me)
71 changes: 71 additions & 0 deletions Distance Calculator/distance_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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!

# Importing module(s)
import math

# Main loop
while True:
use_program=input("Do you want to calculate the distance between two points? (yes/no): ")
if use_program.lower() == "yes":
pass
elif use_program.lower() == "no":
print("Thank you for using the distance calculator!")
quit()
else:
print("Invalid input! Please enter 'yes' or 'no'.")
continue

# Input validation for startX, startY, endX, endY

# startX
while True:
startX = input("Enter starting x-coordinate: ")
if not startX.isnumeric():
print("Error! Input has to be a number!")
continue
else:
break

# startY
while True:
startY = input("Enter starting y-coordinate: ")
if not startY.isnumeric():
print("Error! Input has to be a number!")
continue
else:
break

# endX
while True:
endX = input("Enter ending x-coordinate: ")
if not endX.isnumeric():
print("Error! Input has to be a number!")
continue
else:
break

# endY
while True:
endY = input("Enter ending y-coordinate: ")
if not endY.isnumeric():
print("Error! Input has to be a number!")
continue
else:
break

# Converting the values to floats
startX = float(startX)
startY = float(startY)
endX = float(endX)
endY = float(endY)

# The calculation
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} units.")