From 394985135b43568c199e1c5c70b2696cbb8c669c Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Mon, 27 Jan 2025 20:29:45 +0530 Subject: [PATCH] Adding Distance Calculator (#389) * 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 --- Distance Calculator/README.md | 14 +++++ Distance Calculator/distance_calculator.py | 71 ++++++++++++++++++++++ README.md | 1 + 3 files changed, 86 insertions(+) create mode 100644 Distance Calculator/README.md create mode 100644 Distance Calculator/distance_calculator.py diff --git a/Distance Calculator/README.md b/Distance Calculator/README.md new file mode 100644 index 0000000..4d00f8b --- /dev/null +++ b/Distance Calculator/README.md @@ -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) diff --git a/Distance Calculator/distance_calculator.py b/Distance Calculator/distance_calculator.py new file mode 100644 index 0000000..b5b393c --- /dev/null +++ b/Distance Calculator/distance_calculator.py @@ -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.") diff --git a/README.md b/README.md index c37598b..3754dcc 100644 --- a/README.md +++ b/README.md @@ -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. |