我已经创建了下面的代码,当我导入模块并尝试运行它时,我收到了以下错误:
>>> import aiyoo
>>> aiyoo.bixidist(1,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "aiyoo.py", line 50, in bixidist
currentDist = dist(X,Y,c)
File "aiyoo.py", line 39, in dist
distance = math.sqrt(math.pow((X-getLat(i)),2)+math.pow((Y-getLong(i)),2))
File "aiyoo.py", line 28, in getLat
xmlLat = double(xmlLat)
NameError: global name 'double' is not defined使用double函数是为了将XML的unicode输出转换为double作为后续函数的输入。所以我不明白为什么,当aiyoo模块被导入时,它被认为是一个名称。
下面是名为aiyoo.py的模块:
import math
import urllib2
from xml.dom.minidom import parseString
file = urllib2.urlopen('http://profil.bixi.ca/data/bikeStations.xml')
data = file.read()
file.close()
dom = parseString(data)
#this is how you get the data
def getID(i):
xmlID = dom.getElementsByTagName('id')[i].toxml()
xmlID = xmlID.replace('<id>','').replace('</id>','')
xmlID = int(xmlID)
return xmlID
def getLat(i):
xmlLat = dom.getElementsByTagName('lat')[i].toxml()
xmlLat = xmlLat.replace('<lat>','').replace('</lat>','')
xmlLat = double(xmlLat)
return xmlLat
def getLong(i):
xmlLong = dom.getElementsByTagName('long')[i].toxml()
xmlLong = xmlLong.replace('<long>','').replace('</long>','')
xmlLong = double(xmlLong)
return xmlLong
#this is how we find the distance for a given station
def dist(X,Y,i):
distance = math.sqrt(math.pow((X-getLat(i)),2)+math.pow((Y-getLong(i)),2))
return distance
#this is how we find the closest station
def bixidist(X,Y):
#counter for the lowest
lowDist = 100000
lowIndex = 0
c = 0
end = len(dom.getElementsByTagName('name'))
for c in range(0,end):
currentDist = dist(X,Y,c)
if currentDist < lowDist:
lowIndex = c
lowDist = currentDist
return getID(lowIndex)发布于 2011-11-25 14:37:44
正如其他人回答的那样,double不是python中的内置类型。您必须使用,float。浮点是在C[ ref ]中使用双精度实现的。
至于你问题的主要部分,即“为什么双字词被认为是全局名称?”,当你使用一个variable-name,比如说double,这在本地上下文中找不到,下一次查找是在全局上下文中。然后,如果即使在全局上下文中也找不到它,就会引发异常,比如NameError: global name 'double' is not defined。
祝你编码愉快。
发布于 2011-11-25 14:25:04
Python中没有double类型。如果您查看错误,它会报告找不到名为double的任何内容。Python中的浮点类型被命名为float。
发布于 2011-11-25 14:23:39
https://stackoverflow.com/questions/8265410
复制相似问题