|
| 1 | +# Uses the pythagorean theorem to calculate the distance between two points on a 2D plane. |
| 2 | +# The points are represented as tuples of two numbers. |
| 3 | +# The function should return a float. |
| 4 | + |
| 5 | +# Sample :- startX = 0, startY = 0, endX = 3, endY = 4. |
| 6 | +# So, according to pythagorean theorem, the distance between these two points is 5.0. |
| 7 | + |
| 8 | +# Hope this helps! |
| 9 | + |
| 10 | +# Importing module(s) |
| 11 | +import math |
| 12 | +# Calculate distance |
| 13 | +def calculate_distance(startX, startY, endX, endY): |
| 14 | + return math.sqrt(math.pow(startX - endX, 2) + math.pow(startY - endY, 2)) |
| 15 | + |
| 16 | +# Main loop |
| 17 | +def main(): |
| 18 | + while True: |
| 19 | + print("Welcome to the distance calculator v1.0.1!") |
| 20 | + use_program=input("Do you want to calculate the distance between two points? (yes/no): ") |
| 21 | + if use_program.lower() == "yes": |
| 22 | + pass |
| 23 | + elif use_program.lower() == "no": |
| 24 | + print("Thank you for using the distance calculator!") |
| 25 | + quit() |
| 26 | + else: |
| 27 | + print("Invalid input! Please enter 'yes' or 'no'.") |
| 28 | + continue |
| 29 | + |
| 30 | + # Input validation for startX, startY, endX, endY |
| 31 | + |
| 32 | + # startX |
| 33 | + while True: |
| 34 | + startX = input("Enter starting x-coordinate: ") |
| 35 | + if not startX.isnumeric(): |
| 36 | + print("Error! Input has to be a number!") |
| 37 | + continue |
| 38 | + else: |
| 39 | + break |
| 40 | + |
| 41 | + # startY |
| 42 | + while True: |
| 43 | + startY = input("Enter starting y-coordinate: ") |
| 44 | + if not startY.isnumeric(): |
| 45 | + print("Error! Input has to be a number!") |
| 46 | + continue |
| 47 | + else: |
| 48 | + break |
| 49 | + |
| 50 | + # endX |
| 51 | + while True: |
| 52 | + endX = input("Enter ending x-coordinate: ") |
| 53 | + if not endX.isnumeric(): |
| 54 | + print("Error! Input has to be a number!") |
| 55 | + continue |
| 56 | + else: |
| 57 | + break |
| 58 | + |
| 59 | + # endY |
| 60 | + while True: |
| 61 | + endY = input("Enter ending y-coordinate: ") |
| 62 | + if not endY.isnumeric(): |
| 63 | + print("Error! Input has to be a number!") |
| 64 | + continue |
| 65 | + else: |
| 66 | + break |
| 67 | + |
| 68 | + # Converting the values to floats |
| 69 | + startX = float(startX) |
| 70 | + startY = float(startY) |
| 71 | + endX = float(endX) |
| 72 | + endY = float(endY) |
| 73 | + |
| 74 | + # The calculation |
| 75 | + distance = calculate_distance(startX, startY, endX, endY) |
| 76 | + print(f"The distance between ({startX}, {startY}) and ({endX}, {endY}) is {distance} units.") |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments