2013-11-04 88 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])) 

希望這會有所幫助!