python - function to list earthquakes -


my function designed take data website , collect latitude, longitude, depth, , magnitude of earthquake. second function 'colorcode' supposed take depth of earthquake , return color value it. stuck. tried make data float use if statement , compare int still says cannot converted float. thoughts? (excuse me if post code in improper format)

import urllib  #parseearthquake: int --> list-of-float  def parseearthquakedata(numberofearthquakes):     urlonweb = urllib.urlopen("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv")     lines = urlonweb.readlines()     numberoflines = len(lines)     numberoftimes = 0     index = 1     myaccumalator = []     numberoftimes in range(numberofearthquakes):         while index < (numberofearthquakes + 1):             line = lines[index]             data = line.split(",")             latitude = float(data[1])             longitude = float(data[2])             depth = float(data[3])             magnitude = float(data[4])             if magnitude < 2.5:                 magnitude = 2.5             mylist = [[latitude, longitude, depth, magnitude]]             myaccumalator = myaccumalator + mylist             index = index + 1         return(myaccumalator)   #return [latitude, longitude, depth, magnitude]  def colorcode(numberofquakes):     data = parseearthquakedata(2)     data =str(data)     realdata = data.split()     if realdata[2] <34:         print 'orange'         if realdata[2] >=34<70:             print 'yellow'             if realdata[2] >=70<150:                 print 'green'                 if realdata[2] >=150<300:                     print 'blue'                     if realdata[2] >=300<500:                         print 'purple'                         if realdata[2] >=500:                             print 'red' 

python has convenient csv.dictreader module trying achieve. changed code around bit make shorter , more readbale:

import urllib import csv  #parseearthquake: int --> list-of-float  def parseearthquakedata(numberofearthquakes):     urlonweb = urllib.urlopen("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv")     lines = urlonweb.readlines()     content = csv.dictreader(lines)     allearthquakes = [line line in content]     return allearthquakes[:numberofearthquakes]   #return [latitude, longitude, depth, magnitude]  def colorcode(numberofquakes):     data = parseearthquakedata(2)     earthquake in data:         depth = float(earthquake["depth"])         print(depth)         if depth <34:             print 'orange'         elif 34 <= depth <70:             print 'yellow'         if 70 <= depth <150:             print 'green' 

the dictreader converts every line dictionary corresponding headers csv file. doing because structured data more readible in dictionary compared list (you don't have remember position what).

you had problems number comparison in color code method, compare yours changed to. cannot tell wanted achieve data =str(data)

i did not implement whole color logic should able adapt needs.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -