-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added program * Delete distance_calculator.py * Create README.md * Added program * Updated README.md with program * Added example to README.md * Fixed bugs and added feature Fixed a bug where negative numbers were not allowed and added quit feature to quit the program * Fixed floating point bug Made the calculator compatible with floating(decimal)numbers * Added better input validation
- Loading branch information
1 parent
e012b10
commit 3949851
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Program | ||
This is a *distance calculator*, used to calculate the distance between two points on a 2D plane. | ||
|
||
# How to use | ||
|
||
## Use any of the following commands - | ||
`python distance_calculator.py` | ||
|
||
`python3 distance_calculator.py` | ||
|
||
## Example | ||
![image](https://github.com/user-attachments/assets/987537c2-236e-4560-9a06-3de4bbe98f5e) | ||
|
||
Made by Mathdallas_code(me) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# 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 | ||
|
||
# Main loop | ||
while True: | ||
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 = 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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters