簡體   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

我編寫了一個程序,其中用戶輸入被視為坐標(X 代表緯度,Y 代表經度),距離是根據 Haversine 公式計算的。 在引入浮點坐標之前一切正常:16.34 等數字。

我們試圖處理驗證過程,最明顯的選擇似乎是非isnumeric (允許每種類型的數字)。 但是,在使用小數調用 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

我們正在使用 PyScript,這就是我使用getElementById記錄 x 和 y 的原因。

我將不勝感激任何幫助!

Python 的isnumeric專門檢查字符串中的所有單個字符是否都是數字,而不是字符串是否包含像浮點一樣的“數字”。

您想要的是嘗試將數字轉換為float ,如果其中任何一個無效,則return False

這是帶有try/except塊的代碼,它嘗試將所有輸入轉換為float ,如果您收到ValueError ,則表示其中一個無效。

代碼的 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