From 4c874d1fdecabe7f74ba7b405eb3ed55d6a4daed Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:53:40 +0530 Subject: [PATCH 1/5] Created a README.md for the program --- Distance Calculator/README.md | 14 ++++++++++++++ 1 file changed, 14 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..4d00f8b1 --- /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) From 719c896214e9cbd7f26bad507deca3222ffa4788 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:54:46 +0530 Subject: [PATCH 2/5] Added the program --- Distance Calculator/distance_calculator.py | 35 ++++++++++++++++++++++ 1 file changed, 35 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..14b3bea7 --- /dev/null +++ b/Distance Calculator/distance_calculator.py @@ -0,0 +1,35 @@ + +# 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}.") From d4f046a8d5f254dfd8a189ac2690c89262a58c8c Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:58:41 +0530 Subject: [PATCH 3/5] 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 | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Distance Calculator/distance_calculator.py b/Distance Calculator/distance_calculator.py index 14b3bea7..6fe92139 100644 --- a/Distance Calculator/distance_calculator.py +++ b/Distance Calculator/distance_calculator.py @@ -1,4 +1,3 @@ - # 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. @@ -10,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 From e6ab9ef6e2ee3bda7ff85471f299614e8014b183 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:28:25 +0530 Subject: [PATCH 4/5] 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 201ab647734b7f272e2e85ab54795f0e7f8a79fe Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:33:02 +0530 Subject: [PATCH 5/5] 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.")