【发布时间】:2019-03-08 11:33:09
【问题描述】:
我想制作一个程序,您可以在其中输入您的地址(代码中的示例是荷兰地址),然后程序将输出该地址的经度和纬度。我还尝试使它对用户更友好,所以如果输入的地址不存在,程序会这样说。 代码是:
from geopy.geocoders import ArcGIS
nom = ArcGIS()
adres = input("enter your adress as folows:\n 32 Gunterstein, Amsterdam, 1081 CJ\n vul in: ")
n = nom.geocode(adres)
if adres in nom:
print("longitude:",n.longitude)
print("latitude:", n.latitude)
else:
print("adress doesn't exist, please try again.")
print("end")
如果用户输入了一个有效的地址,代码就可以工作,但是当我通过输入废话来尝试时,我得到以下错误:
enter your adress as folows:
32 Gunterstein, Amsterdam, 1081 CJ
vul in: nonsense
Traceback (most recent call last):
File "breede_en_lengte_graden.py", line 7, in <module>
if adres in nom:
TypeError: argument of type 'ArcGIS' is not iterable
我收到该错误的代码有什么问题?
谢谢!
【问题讨论】:
-
也许尝试使用
try和except而不是if else? -
你理解
if adres in nom应该怎么做? -
@FlyingTeller 我认为 if 会检查地址是否有效,但是当我查看问题的答案时,情况并非如此