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