我写了一个程序,可以使用google的api找到两点之间的距离。我对大多数问题进行了排序,但只有一个问题是回忆,即如果用户输入任何随机单词和数字,那么我收到错误'NoneType'对象没有属性'纬度'。
基本上我想检查用户是否仅输入城市名称或城市名称,状态(如MA,NY等),程序应拒绝其余输入。
我该如何解决这个问题?
from geopy.geocoders import Nominatim
from geopy.distance import vincenty
from geopy.distance import great_circle
geolocator = Nominatim()
try:
#Asking user for input
Location = raw_input('Enter the Final location in the format of ctiyname or cityname state name, please avoid "," to separte cityname and statename :-')
#New line char
print "\n"
except:
#Printing error message
print "location not found"
#Using "raise SystemExit" for program termination if file not found
raise SystemExit
location_2 = geolocator.geocode(Location)
#Getting the coordinates of users location
coord_2 = (location_2.latitude, location_2.longitude)
#using static location
Location_1 = 'Hoboken'
#static location coordinates
coord_1 = (40.7433066, -74.0323751)
print "The static location is",Location_1,"and its geographical coordinates are",coord_1,"\n"
print "The target location is ",Location,"and its geographical coordinates are",coord_2,"\n"
print "The distance between the two destinations by 'Vincenty distance' method in miles is :",(vincenty (coord_1, coord_2).miles),"\n"
print "The distance between the two destinations by 'great_circle'method in miles is :",(great_circle (coord_1, coord_2).miles),"\n"