2013-11-04 83 views
1

我需要做一个这样的功能。 (Python 3.3顺便说一下)Python:cTurtle与列表清单

为程序写合同,文档字符串和实现plotEarthquakeData在给定日期和世界地图上的点之间绘制两个日期并绘制来自USGS的所有地震数据。你可以使用cTurtle库中的程序点,它取得了大小和颜色。您可以使用4的乘积和点的大小的大小,而使用正确颜色的深度。 bgpic程序对于将世界地图图像置于背景中很有用,而setWorldCoordinates程序可以帮助您更轻松地绘制点。 假设整个地图从左到右显示-180到180度,从下到上显示-90到90度。

plotEarthquakeData("2013/06/01", "2013/06/04") should look like this

我有这个至今。在它下面的是我已经写过的函数,它也将在plotEarthquakeData函数中使用。

import cTurtle 

def plotEarthquakeData(date1,date2): 
    """ takes two dates and plots all the earthquake data from USGS between the 
    given dates with dots on the world map.""" 
    myTurtle = cTurtle.Turtle() 
    myTurtle.bgpic('map.gif') 
    myTurtle.setWorldCoordinates(-180,-90,180,90) 
    data = parseEarthquakeData(date1,date2) 
    for i in range (len(data[1])): 
     myTurtle.goto(data[0][i], data[1][i]) 
     myturtle.dot(4*data[2][i],colorCode(data[3][1])) 

-

def colorCode(depth): 
    """takes the depth of an earthquake and returns the corresponding color for 
    the earthquake.""" 
    if depth<=33: 
     return "orange" 
    elif depth<=70: 
     return "yellow" 
    elif depth <=150: 
     return "green" 
    elif depth<=300: 
     return "blue" 
    elif depth <=500: 
     return "purple" 
    else: 
     return "red" 

-

import urllib.request 
def parseEarthquakeData(date1, date2): 
    dataFile = urllib.request.urlopen("http://neic.usgs.gov/neis/gis/qed.asc") 
    latList = [] 
    longList = [] 
    magList = [] 
    depthList = [] 
    count =0 
    for aline in dataFile: 
     aline = aline.decode('ascii') 
     splitData = aline.split(',') 
     count = count+1 
     if count>=2: 

      if (betweenDates (splitData[0],date1,date2)): 
       latList.append(splitData[2]) 
       longList.append(splitData[3]) 
       magList.append(splitData[4]) 
       depthList.append(splitData[5]) 
    finalList=[] 
    finalList.append(latList) 
    finalList.append(longList) 
    finalList.append(magList) 
    finalList.append(depthList) 
    return finalList 

当我尝试运行plotEarthquakeData我得到这个错误,我不知道做什么。

plotEarthquakeData("2013/06/01","2013/06/04") 
Traceback (most recent call last): 
    File "<pyshell#2>", line 1, in <module> 
    plotEarthquakeData("2013/06/01","2013/06/04") 
    File "C:\Python33\plotEarthquakes.py", line 89, in plotEarthquakeData 
    myTurtle.goto(data[0][i], data[1][i]) 
    File "C:\Python33\lib\site-packages\cTurtle.py", line 1295, in setpos 
    self._goto(_Vec(pos, y)) 
    File "C:\Python33\lib\site-packages\cTurtle.py", line 2255, in _goto 
    diff = end-start 
    File "C:\Python33\lib\site-packages\cTurtle.py", line 274, in __sub__ 
    return _Vec(self[0]-other[0], self[1]-other[1]) 
TypeError: unsupported operand type(s) for -: 'str' and 'float' 

所以任何帮助,试图让我明白我要去哪里错了将会非常赞赏

回答

1

latlistlonglist包含一个字符串,而不是数量,因为splitData是一个字符串。

你必须将它们转换为浮动,如果你想这样做:

if (betweenDates (splitData[0],date1,date2)): 
    latList.append(float(splitData[2])) 
    longList.append(float(splitData[3])) 

我希望其他变量是花车太,不是吗?

magList.append(float(splitData[4])) 
    depthList.append(float(splitData[5])) 

希望这会有所帮助!