Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Adding Distance Calculator #822

Open
wants to merge 2 commits into
base: main
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
17 changes: 17 additions & 0 deletions projects/Distance Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1 style="text-align: center">Distance Calculator</h1>
This is a distance calculator made using Python. It can calculate the distance between two points on a 2D plane.

## How to use

`python3 distance_calculator.py`

There are two functions, `main()` and `calculate_distance()`

By default, if you run the project it runs the `main()` function.
You can download the file to use it as a module.

## Other

I am planning to make a PyPI package of it soon.

You can view it's repository [here](https://github.com/Mathdallas-code/Distance-Calculator)
79 changes: 79 additions & 0 deletions projects/Distance Calculator/distance_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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
# Calculate distance
def calculate_distance(startX, startY, endX, endY):
return math.sqrt(math.pow(startX - endX, 2) + math.pow(startY - endY, 2))

# Main loop
def main():
while True:
print("Welcome to the distance calculator v1.0.1!")
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 = calculate_distance(startX, startY, endX, endY)
print(f"The distance between ({startX}, {startY}) and ({endX}, {endY}) is {distance} units.")

if __name__ == "__main__":
main()