Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 62 additions & 55 deletions distance_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,71 @@

# 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
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()
Loading