Python:带有列表列表的 cTurtle

标签 python list

我需要创建一个函数来执行此操作。 (顺便说一句,Python 3.3)

Write the contract, docstring, and implementation for a procedure plotEarthquakeData that takes two dates and plots all the earthquake data from USGS between the given dates with dots on the world map. You can use the procedure dot from the cTurtle library that takes the size and color. You can use the product of 4 and the magnitude for the size of dots while using the depth for the right color. The procedure bgpic is useful to put the world map image in the background while the procedure setWorldCoordinates can help you plot the dots more easily. Assume the entire map shows -180 to 180 degrees from left to right and -90 to 90 degrees from bottom to top.

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'

因此,任何帮助我理解我哪里出错的帮助都将非常感激

最佳答案

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

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

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

我希望其他变量也是 float ,不是吗?

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

希望这有帮助!

关于Python:带有列表列表的 cTurtle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759845/

相关文章:

Java ArrayList removeAll() 不删除

python - 使用 python 和 flask 的多个 SQL 查询

python - 在python中定义未知大小的矩阵

c - 链表麻烦

java - 将项目动态添加到 List<Integer> 并转换为 Integer[] 数组

javascript - 循环遍历数组并打印特定范围的值?

Python 正则表达式字边界未按预期工作

Python - 缩短冗余循环

python - 如何在 Python 中使用 boolean 值?

java - ListView 不响应点击/条件问题