diff --git a/projects/Distance Calculator/README.md b/projects/Distance Calculator/README.md new file mode 100644 index 00000000..b68fb675 --- /dev/null +++ b/projects/Distance Calculator/README.md @@ -0,0 +1,17 @@ +

Distance Calculator

+This is a distance calculator made using Python. It can calculate the distance between two points on a 2D plane. + +## How to use + +`python3 distance_calculator.py` + +There are two functions, `main()` and `calculate_distance()` + +By default, if you run the project it runs the `main()` function. +You can download the file to use it as a module. + +## Other + +I am planning to make a PyPI package of it soon. + +You can view it's repository [here](https://github.com/Mathdallas-code/Distance-Calculator) diff --git a/projects/Distance Calculator/distance_calculator.py b/projects/Distance Calculator/distance_calculator.py new file mode 100644 index 00000000..a9607d99 --- /dev/null +++ b/projects/Distance Calculator/distance_calculator.py @@ -0,0 +1,79 @@ +# 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 +# Calculate distance +def calculate_distance(startX, startY, endX, endY): + return math.sqrt(math.pow(startX - endX, 2) + math.pow(startY - endY, 2)) + +# Main loop +def main(): + 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 + 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.") + +if __name__ == "__main__": + main()