From 2786243c2c3e228d08cd6e07168f9e37e2c28ebe Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Sun, 12 Jan 2025 12:27:25 +0530 Subject: [PATCH 1/9] Added program --- distance_calculator.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 distance_calculator.py diff --git a/distance_calculator.py b/distance_calculator.py new file mode 100644 index 00000000..38cbc5fc --- /dev/null +++ b/distance_calculator.py @@ -0,0 +1,34 @@ +# 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! +import math + +while True: + startX = input("Enter starting x-coordinate: ") + if not startX.isdigit(): + print("Error! Input has to be a number!") + continue + startY = input("Enter starting y-coordinate: ") + if not startY.isdigit(): + print("Error! Input has to be a number!") + continue + endX = input("Enter ending x-coordinate: ") + if not endX.isdigit(): + print("Error! Input has to be a number!") + continue + endY = input("Enter ending y-coordinate: ") + if not endY.isdigit(): + print("Error! Input has to be a number!") + continue + + startX = int(startX) + startY = int(startY) + endX = int(endX) + endY = int(endY) + 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}.") \ No newline at end of file From 30ad5bbf37a66fb28163e057db47a7ee0b8c2cf0 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Sun, 12 Jan 2025 12:46:31 +0530 Subject: [PATCH 2/9] Delete distance_calculator.py --- distance_calculator.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 distance_calculator.py diff --git a/distance_calculator.py b/distance_calculator.py deleted file mode 100644 index 38cbc5fc..00000000 --- a/distance_calculator.py +++ /dev/null @@ -1,34 +0,0 @@ -# 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! -import math - -while True: - startX = input("Enter starting x-coordinate: ") - if not startX.isdigit(): - print("Error! Input has to be a number!") - continue - startY = input("Enter starting y-coordinate: ") - if not startY.isdigit(): - print("Error! Input has to be a number!") - continue - endX = input("Enter ending x-coordinate: ") - if not endX.isdigit(): - print("Error! Input has to be a number!") - continue - endY = input("Enter ending y-coordinate: ") - if not endY.isdigit(): - print("Error! Input has to be a number!") - continue - - startX = int(startX) - startY = int(startY) - endX = int(endX) - endY = int(endY) - 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}.") \ No newline at end of file From a9fbeb7fc7a4cb07e997bb900805c7b3d90f0b12 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Sun, 12 Jan 2025 12:53:19 +0530 Subject: [PATCH 3/9] Create README.md --- Distance Calculator/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Distance Calculator/README.md diff --git a/Distance Calculator/README.md b/Distance Calculator/README.md new file mode 100644 index 00000000..71bf1a99 --- /dev/null +++ b/Distance Calculator/README.md @@ -0,0 +1,11 @@ +# 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` + +Made by Mathdallas_code(me) From d8a609126a70ae769ccc9d5e97dacf5b5495476b Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Sun, 12 Jan 2025 12:53:59 +0530 Subject: [PATCH 4/9] Added program --- Distance Calculator/distance_calculator.py | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Distance Calculator/distance_calculator.py diff --git a/Distance Calculator/distance_calculator.py b/Distance Calculator/distance_calculator.py new file mode 100644 index 00000000..38cbc5fc --- /dev/null +++ b/Distance Calculator/distance_calculator.py @@ -0,0 +1,34 @@ +# 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! +import math + +while True: + startX = input("Enter starting x-coordinate: ") + if not startX.isdigit(): + print("Error! Input has to be a number!") + continue + startY = input("Enter starting y-coordinate: ") + if not startY.isdigit(): + print("Error! Input has to be a number!") + continue + endX = input("Enter ending x-coordinate: ") + if not endX.isdigit(): + print("Error! Input has to be a number!") + continue + endY = input("Enter ending y-coordinate: ") + if not endY.isdigit(): + print("Error! Input has to be a number!") + continue + + startX = int(startX) + startY = int(startY) + endX = int(endX) + endY = int(endY) + 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}.") \ No newline at end of file From d028b7c25dd7b062924eb5a04e1dd677e9bef3f7 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:18:49 +0530 Subject: [PATCH 5/9] Updated README.md with program --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fbebef09..e138a31e 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. | From f40a210a9ac7834dbd5cb35820a4f318e685addf Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:43:46 +0530 Subject: [PATCH 6/9] Added example to README.md --- Distance Calculator/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Distance Calculator/README.md b/Distance Calculator/README.md index 71bf1a99..4d00f8b1 100644 --- a/Distance Calculator/README.md +++ b/Distance Calculator/README.md @@ -8,4 +8,7 @@ This is a *distance calculator*, used to calculate the distance between two poin `python3 distance_calculator.py` +## Example +![image](https://github.com/user-attachments/assets/987537c2-236e-4560-9a06-3de4bbe98f5e) + Made by Mathdallas_code(me) From 9c5c79d78b37f90f1e3262486ef3b9d2bef788ce Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:57:42 +0530 Subject: [PATCH 7/9] Fixed bugs and added feature Fixed a bug where negative numbers were not allowed and added quit feature to quit the program --- Distance Calculator/distance_calculator.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Distance Calculator/distance_calculator.py b/Distance Calculator/distance_calculator.py index 38cbc5fc..6fe92139 100644 --- a/Distance Calculator/distance_calculator.py +++ b/Distance Calculator/distance_calculator.py @@ -9,20 +9,30 @@ import math while True: + que=input("Do you want to calculate the distance between two points? (yes/no): ") + if que.lower() == "yes": + pass + elif que.lower() == "no": + print("Thank you for using the distance calculator!") + quit() + else: + print("Invalid input! Please enter 'yes' or 'no'.") + continue + startX = input("Enter starting x-coordinate: ") - if not startX.isdigit(): + if startX.isalpha(): print("Error! Input has to be a number!") continue startY = input("Enter starting y-coordinate: ") - if not startY.isdigit(): + if startY.isalpha(): print("Error! Input has to be a number!") continue endX = input("Enter ending x-coordinate: ") - if not endX.isdigit(): + if endX.isalpha(): print("Error! Input has to be a number!") continue endY = input("Enter ending y-coordinate: ") - if not endY.isdigit(): + if endY.isalpha(): print("Error! Input has to be a number!") continue @@ -31,4 +41,4 @@ endX = int(endX) endY = int(endY) 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}.") \ No newline at end of file + print(f"The distance between ({startX}, {startY}) and ({endX}, {endY}) is {distance}.") From 60018f8d90437723955bf902b4f08d4b1c5effe3 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:29:14 +0530 Subject: [PATCH 8/9] Fixed floating point bug Made the calculator compatible with floating(decimal)numbers --- Distance Calculator/distance_calculator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Distance Calculator/distance_calculator.py b/Distance Calculator/distance_calculator.py index 6fe92139..82288e75 100644 --- a/Distance Calculator/distance_calculator.py +++ b/Distance Calculator/distance_calculator.py @@ -36,9 +36,9 @@ print("Error! Input has to be a number!") continue - startX = int(startX) - startY = int(startY) - endX = int(endX) - endY = int(endY) + startX = float(startX) + startY = float(startY) + endX = float(endX) + endY = float(endY) 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}.") From f8f99cce88af92c0f17f21934d853f3759acfab8 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:31:56 +0530 Subject: [PATCH 9/9] Added better input validation --- Distance Calculator/distance_calculator.py | 67 +++++++++++++++------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/Distance Calculator/distance_calculator.py b/Distance Calculator/distance_calculator.py index 82288e75..b5b393c8 100644 --- a/Distance Calculator/distance_calculator.py +++ b/Distance Calculator/distance_calculator.py @@ -6,39 +6,66 @@ # 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: - que=input("Do you want to calculate the distance between two points? (yes/no): ") - if que.lower() == "yes": + use_program=input("Do you want to calculate the distance between two points? (yes/no): ") + if use_program.lower() == "yes": pass - elif que.lower() == "no": + elif use_program.lower() == "no": print("Thank you for using the distance calculator!") quit() else: print("Invalid input! Please enter 'yes' or 'no'.") continue - startX = input("Enter starting x-coordinate: ") - if startX.isalpha(): - print("Error! Input has to be a number!") - continue - startY = input("Enter starting y-coordinate: ") - if startY.isalpha(): - print("Error! Input has to be a number!") - continue - endX = input("Enter ending x-coordinate: ") - if endX.isalpha(): - print("Error! Input has to be a number!") - continue - endY = input("Enter ending y-coordinate: ") - if endY.isalpha(): - print("Error! Input has to be a number!") - 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}.") + print(f"The distance between ({startX}, {startY}) and ({endX}, {endY}) is {distance} units.")