From 51eabc23d313058eed44f4d7d8d6ff3542466536 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Fri, 4 Apr 2025 14:36:10 +0530 Subject: [PATCH 1/3] Update distance_calculator.py Added a main function so it only runs if run form the main file --- distance_calculator.py | 117 ++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 55 deletions(-) diff --git a/distance_calculator.py b/distance_calculator.py index e02d934..b0c9c29 100644 --- a/distance_calculator.py +++ b/distance_calculator.py @@ -9,64 +9,71 @@ # Importing module(s) import math +# Calculate distance +def calculate_distance(start_x, start_y, end_x, end_y): + return math.sqrt(math.pow(startX - endX, 2) + math.pow(startY - endY, 2)) # Main loop -while True: - print("Welcome to the distance calculator v1.0.1!") - 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 +def main(): while True: - startX = input("Enter starting x-coordinate: ") - if not startX.isnumeric(): - print("Error! Input has to be a number!") - continue + print("Welcome to the distance calculator v1.0.1!") + 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: - break + print("Invalid input! Please enter 'yes' or 'no'.") + continue - # 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) + # 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 = calculate_distance(startX, startY, endX, endY) + print(f"The distance between ({startX}, {startY}) and ({endX}, {endY}) is {distance} units.") - # 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.") +if "__name__" == __main__: + main() From af89da468eed8d9c99378f3510c09bcc78e0a7f5 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Fri, 4 Apr 2025 14:38:02 +0530 Subject: [PATCH 2/3] Update distance_calculator.py --- distance_calculator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distance_calculator.py b/distance_calculator.py index b0c9c29..17bae0b 100644 --- a/distance_calculator.py +++ b/distance_calculator.py @@ -75,5 +75,5 @@ def main(): distance = calculate_distance(startX, startY, endX, endY) print(f"The distance between ({startX}, {startY}) and ({endX}, {endY}) is {distance} units.") -if "__name__" == __main__: +if __name__ == "__main__": main() From a8d6bef8626dde0094bf64d4c465a53db1e2b895 Mon Sep 17 00:00:00 2001 From: Jaiwant <158043502+Mathdallas-code@users.noreply.github.com> Date: Fri, 4 Apr 2025 14:40:35 +0530 Subject: [PATCH 3/3] Update distance_calculator.py --- distance_calculator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distance_calculator.py b/distance_calculator.py index 17bae0b..a9607d9 100644 --- a/distance_calculator.py +++ b/distance_calculator.py @@ -10,7 +10,7 @@ # Importing module(s) import math # Calculate distance -def calculate_distance(start_x, start_y, end_x, end_y): +def calculate_distance(startX, startY, endX, endY): return math.sqrt(math.pow(startX - endX, 2) + math.pow(startY - endY, 2)) # Main loop