Skip to content

Commit

Permalink
Adding Distance Calculator (#389)
Browse files Browse the repository at this point in the history
* Added program

* Delete distance_calculator.py

* Create README.md

* Added program

* Updated README.md with program

* Added example to README.md

* Fixed bugs and added feature

Fixed a bug where negative numbers were not allowed and added quit feature to quit the program

* Fixed floating point bug

Made the calculator compatible with floating(decimal)numbers

* Added better input validation
  • Loading branch information
Mathdallas-code authored Jan 27, 2025
1 parent e012b10 commit 3949851
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
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.")
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ More information on contributing and the general code of conduct for discussion
| Currency Script | [Currency Script](https://github.com/DhanushNehru/Python-Scripts/tree/master/Currency%20Script) | A Python script to convert the currency of one country to that of another. |
| Digital Clock | [Digital Clock](https://github.com/DhanushNehru/Python-Scripts/tree/master/Digital%20Clock) | A Python script to preview a digital clock in the terminal. |
| Display Popup Window | [Display Popup Window](https://github.com/DhanushNehru/Python-Scripts/tree/master/Display%20Popup%20Window) | A Python script to preview a GUI interface to the user. |
| Distance Calculator | [Distance Calculator](https://github.com/Mathdallas-code/Python-Scripts/tree/master/Distance%20Calculator) | A Python script to calculate the distance between two points.
| Duplicate Finder | [Duplicate Finder](https://github.com/DhanushNehru/Python-Scripts/tree/master/Duplicate%Fnder) | The script identifies duplicate files by MD5 hash and allows deletion or relocation. |
| Emoji | [Emoji](https://github.com/DhanushNehru/Python-Scripts/tree/master/Emoji) | The script generates a PDF with an emoji using a custom TrueType font. |
| Emoji to PDF | [Emoji to PDF](https://github.com/DhanushNehru/Python-Scripts/tree/master/Emoji%20To%20Pdf) | A Python Script to view Emoji in PDF. |
Expand Down

0 comments on commit 3949851

Please sign in to comment.