简体   繁体   English

验证用户输入作为点的坐标,并确保它可以是 integer,同时浮动或负

[英]Validate the user input as coordinate for point and make sure that it can be an integer, float or negative at the same time

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.我编写了一个程序,其中用户输入被视为坐标(X 代表纬度,Y 代表经度),距离是根据 Haversine 公式计算的。 Everything works until float coordinates are introduced: numbers such as 16.34.在引入浮点坐标之前一切正常: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).我们试图处理验证过程,最明显的选择似乎是非isnumeric (允许每种类型的数字)。 However, upon calling X or Y with decimals, the program goes to the False statement!但是,在使用小数调用 X 或 Y 时,程序会转到 False 语句!

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 .我们正在使用 PyScript,这就是我使用getElementById记录 x 和 y 的原因。

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. Python 的isnumeric专门检查字符串中的所有单个字符是否都是数字,而不是字符串是否包含像浮点一样的“数字”。

What you'd want is to try to convert the numbers to float and if any of them are invalid, then return False .您想要的是尝试将数字转换为float ,如果其中任何一个无效,则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.这是带有try/except块的代码,它尝试将所有输入转换为float ,如果您收到ValueError ,则表示其中一个无效。

The rest of the code then uses these float -converted values.代码的 rest 然后使用这些float转换的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM