想要创建一个能够读取具有纬度和经度的动物名称文件的函数,它将返回设置区域内的#animals,但是我会一直得到一个Index错误,我不知道为什么,我仍然Python的新手,只需要一些帮助:')
def LocationCount(FileName, Distance, Lat2, Lon2):
List =[]
File = open(FileName, "r")
for line in File:
List.append(LineToList(line))
File.close()
List2 =[]
ListCount = 0
while ListCount <= len(List):
if CalculateDistance(List[ListCount][1], List[ListCount][2], Lat2, Lon2) <= Distance:
List2.append(line)
ListCount += 1
return(List2)
S = LocationCount("Mammal.txt", 10, 54.988056, -1.619444)
print (len(List2))
答案 0 :(得分:1)
while ListCount <= len(List):
将其更改为,
while ListCount < len(List):
列表为0索引。
此外,您将值存储在S中并尝试打印list2,这在此范围内未定义。
S = LocationCount("Mammal.txt", 10, 54.988056, -1.619444)
print (len(List2))
将其替换为
S = LocationCount("Mammal.txt", 10, 54.988056, -1.619444)
print len(S)