From d39f5521aa8a1ac01ca5d195fff058bb46e26c91 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Sat, 11 Jan 2025 19:21:32 +0530 Subject: [PATCH 1/4] Add files via upload --- 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 0f788de535670783fdd0e78f03ff7b8191520ff8 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:55:46 +0530 Subject: [PATCH 2/4] Fixed bugs and added feature Fixed a bug where negative numbers were not allowed and added a quit features --- distance_calculator.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/distance_calculator.py b/distance_calculator.py index 38cbc5fc..6fe92139 100644 --- a/distance_calculator.py +++ b/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 80090704a209077383ec9d41bbef46b66bc1492c Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:30:14 +0530 Subject: [PATCH 3/4] Fixed floating point bug Made the calculator compatible with floating(decimal)numbers --- distance_calculator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/distance_calculator.py b/distance_calculator.py index 6fe92139..82288e75 100644 --- a/distance_calculator.py +++ b/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 970fd6da3b91f1c1f644e7cff7819aeee25ee2ce Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:30:59 +0530 Subject: [PATCH 4/4] Added better input validation --- distance_calculator.py | 67 +++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/distance_calculator.py b/distance_calculator.py index 82288e75..b5b393c8 100644 --- a/distance_calculator.py +++ b/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.")