Difference between revisions of "CSC111 How cold is it in England?"

From dftwiki3
Jump to: navigation, search
 
(2 intermediate revisions by the same user not shown)
Line 27: Line 27:
 
           'Sheffield'  : 'sheffielddata.txt' }
 
           'Sheffield'  : 'sheffielddata.txt' }
  
def getCity( cityName ):
+
def getCity( fileName ):
 
   global URL
 
   global URL
   print( "Getting list of temperatures from ", url )
+
   print( "Getting list of temperatures from ", URL )
   f = urllib.request.urlopen( URL + cityName )
+
   f = urllib.request.urlopen( URL + fileName )
 
   bytes = f.read()
 
   bytes = f.read()
 
   htmlText  = bytes.decode( "utf8" )
 
   htmlText  = bytes.decode( "utf8" )
Line 46: Line 46:
 
     while True:
 
     while True:
 
         city = input( "> " )
 
         city = input( "> " )
         if city in cities.keys():
+
         if city in cities:
 
             break
 
             break
 
         print( "Sorry,", city,"is not a valid city name" )
 
         print( "Sorry,", city,"is not a valid city name" )

Latest revision as of 09:55, 13 December 2011

--D. Thiebaut 22:57, 12 December 2011 (EST)


# ukTemperatures.py
# D. Thiebaut
# this code is undocumented and possibly not robust...
# Main site: http://www.metoffice.gov.uk/climate/uk/stationdata/
#
import urllib.request
import sys

URL = "http://cs.smith.edu/~111a/climate/uk/stationdata/"
ALTERNATE_URL = "http://cs.smith.edu/dftwiki/DFT/climate/uk/stationdata/"

cities = {'Ross-on-Wye' : 'rossonwyedata.txt',
          'Armagh'      : 'armaghdata.txt',
          'Leuchars'    : 'leucharsdata.txt',
          'Eskdalemuir' : 'eskdalemuirdata.txt',
          'Yeovilton'   : 'yeoviltondata.txt',
          'Valley'      : 'valleydata.txt',
          'Sutton Bonington': 'suttonboningtondata.txt',
          'Dunstaffnage': 'dunstaffnagedata.txt',
          'Shawbury'    : 'shawburydata.txt',
          'Paisley'     : 'paisleydata.txt',
          'Braemar'     : 'braemardata.txt',
          'Sheffield'   : 'sheffielddata.txt' }

def getCity( fileName ):
   global URL
   print( "Getting list of temperatures from ", URL )
   f = urllib.request.urlopen( URL + fileName )
   bytes = f.read()
   htmlText  = bytes.decode( "utf8" )
   return htmlText


def main():
    # prompt the user for a city name
    print( "What city are you interested in? " )

    # display list of cities (keys of dictionary) 
    print( "Choices are: ", ", ".join( cities.keys() ) )

    # keep on asking until city is recognized
    while True:
        city = input( "> " )
        if city in cities:
            break
        print( "Sorry,", city,"is not a valid city name" )

    # get text file associated with city
    cityFile = cities[ city ]

    # get Web page content
    text = getCity( cityFile )
    print( text )

main()




...