I wrote a program where the user input is taken as a coordinate (X for latitude and Y for longitude) and the distance is calculated based on Haversine formula. Everything works until float coordinates are introduced: numbers such as 16.34.
We tried to take care of the validation process, and the most obvious choice seemed to be isnumeric
(to allow every type of number). However, upon calling X or Y with decimals, the program goes to the False statement!
FirstLat = document.getElementById("FirstLat").value
FirstLong= document.getElementById('FirstLong').value
SecLat= document.getElementById('SecLat').value
SecLong= document.getElementById('SecLong').value
#validate input
if(not(FirstLat.lstrip("-").isnumeric()) or not(FirstLong.lstrip("-").isnumeric()) or not(SecLat.lstrip("-").isnumeric()) or not(SecLong.lstrip("-").isnumeric())):
alert("Invalid Input(s), please make sure you have valid coordinates input")
return False
else:
#since the input values will be string, converting to int
dx = float(SecLat) - float(FirstLat)
dy = float(SecLong) - float(FirstLong)
dsquared = dx*dx + dy*dy
calculated_distance = dsquared**0.5
# to calculate with curvature using Haversine Formula
FirstLat = radians(float(FirstLat))
SecLat = radians(float(SecLat))
FirstLong = radians(float(FirstLong))
SecLong = radians(float(SecLong))
# Calculaating using the Haversine formula
dlon = SecLong - FirstLong
dlat = SecLat - FirstLat
a = sin(dlat / 2)**2 + cos(FirstLat) * cos(SecLat) * sin(dlon / 2)**2
c = 2 * asin(sqrt(a))
# We define the radius of the earth in miles
r = 6371
# calculate the result
calculated_distance = c * r
We are using PyScript, and that is why I am recording the x and y with getElementById
.
I would appreciate any help!
Python's isnumeric
specifically checks if all individual characters in the string are numbers, not that the string contains "a number" like a floating point.
What you'd want is to try to convert the numbers to float
and if any of them are invalid, then return False
.
Here's your code with a try/except
block which tries to convert all your input into float
, and if you get a ValueError
it means one of them was invalid.
The rest of the code then uses these float
-converted values.
FirstLat = document.getElementById("FirstLat").value
FirstLong= document.getElementById('FirstLong').value
SecLat= document.getElementById('SecLat').value
SecLong= document.getElementById('SecLong').value
# attempt conversion of input and alert if any of them
# aren't valid conversions
try:
first_lat = float(FirstLat)
first_long = float(FirstLong)
sec_lat = float(SecLat)
sec_long = float(SecLong)
except ValueError:
alert("Invalid inputs, please make sure you have valid coordinates input.")
return False
dx = sec_lat - first_lat
dy = sec_long - first_long
dsquared = dx*dx + dy*dy
calculated_distance = dsquared**0.5
# to calculate with curvature using Haversine Formula
first_lat_rad = radians(first_lat)
sec_lat_rad = radians(sec_lat)
first_long_rad = radians(first_long)
sec_long_rad = radians(sec_long)
# Calculating using the Haversine formula
dlon = sec_long_rad - first_long_rad
dlat = sec_lat_rad - first_lat_rad
a = sin(dlat / 2)**2 + cos(first_lat_rad) * cos(sec_lat_rad) * sin(dlon / 2)**2
c = 2 * asin(sqrt(a))
# We define the radius of the earth in miles
r = 6371
# calculate the result
calculated_distance = c * r