CSC111 Lab 11 Solution Programs 2011
--D. Thiebaut 22:39, 17 November 2011 (EST)
# lab11 solutions
#
import urllib.request
import sys
# base url for the weather.com Web site. User will provide
# zip code.
BASEURL = "http://www.weather.com/weather/today/" # 01060"
statistics1 = [['Afghanistan', 28710000, 'NA', 'NA', 1], ['Albania', 3580000,
12000, 'NA', 10], ['Algeria', 32810000, 180000, 'NA', 2],
['Andorra', 69150, 24500, 'NA', 1], ['Angola', 10760000,
60000, 'NA', 1], ['Anguilla', 12738, 919, 'NA', 16],
['Antigua_and_Barbuda', 67897, 5000, 'NA', 16], ['Argentina',
38740000, 4650000, 'NA', 33], ['Armenia', 3320000, 30000, 'NA',
9], ['Aruba', 70844, 24000, 'NA', 'NA'], ['Australia', 19730000,
13010000, 9020000, 571], ['Austria', 8180000, 4650000,
1300000, 37], ['Azerbaijan', 7830000, 25000, 'NA', 2],
['Bahrain', 667238, 140200, 'NA', 1], ['Bangladesh',
138440000, 150000, 'NA', 10], ['Barbados', 277264, 6000, 'NA',
19], ['Belarus', 10330000, 422000, 'NA', 23], ['Belgium',
10280000, 4870000, 1600000, 61], ['Belize', 266440, 18000,
'NA', 2], ['Benin', 7040000, 25000, 'NA', 4], ['Bhutan',
2130000, 2500, 'NA', 'NA'], ['Bolivia', 8580000, 78000, 'NA', 9],
['Bosnia_and_Herzegovian', 3980000, 45000, 'NA', 3],
['Botswana', 1570000, 33000, 'NA', 11], ['Brazil', 182030000,
22320000, 10860000, 50], ['Brunei', 358098, 35000, 'NA', 2],
['Bulgaria', 7530000, 1610000, 'NA', 200], ['Burkina_Faso',
13220000, 25000, 'NA', 1], ['Burma', 42510000, 10000, 'NA', 1],
['Burundi', 6090000, 6000, 'NA', 1], ['Cambodia', 13120000,
10000, 'NA', 2], ['Cameroon', 15740000, 45000, 'NA', 1] ]
statistics2 = [['Afghanistan', 28710000, 'NA', 'NA', 1], ['Albania', 3580000,
12000, 'NA', 10], ['Algeria', 32810000, 180000, 'NA'],
['Andorra', 69150, 24500, 'NA', 1], ['Angola', 10760000,
60000, 'NA', 1], ['Anguilla', 12738, 919, 'NA', 16],
['Antigua_and_Barbuda', 67897, 5000, 'NA', 16], ['Argentina',
38740000, 4650000, 'NA', 33], ['Armenia', 3320000, 30000, 'NA',
9], ['Aruba', 70844, 24000, 'NA' ], ['Australia', 19730000,
13010000, 9020000, 571], ['Austria', 8180000, 4650000,
1300000, 37], ['Azerbaijan', 7830000, 25000, 'NA'],
['Bahrain', 667238, 140200, 'NA', 1], ['Bangladesh',
138440000, 150000, 'NA', 10], ['Barbados', 277264, 6000, 'NA',
19], ['Belarus', 10330000, 422000, 'NA', 23], ['Belgium',
10280000, 4870000, 1600000, 61], ['Belize', 266440, 18000,
'NA', 2], ['Benin', 7040000, 25000, 'NA', 4], ['Bhutan',
2130000, 2500, 'NA', 'NA'], ['Bolivia', 8580000, 78000, 'NA', 9],
['Bosnia_and_Herzegovian', 3980000, 45000, 'NA', 3],
['Botswana', 1570000, 33000, 'NA', 11], ['Brazil', 182030000,
22320000, 10860000, 50], ['Brunei', 358098, 35000, 'NA', 2],
['Bulgaria', 7530000, 1610000, 'NA', 200], ['Burkina_Faso',
13220000, 25000, 'NA'], ['Burma', 42510000, 10000, 'NA', 1],
['Burundi', 6090000, 6000, 'NA', 1], ['Cambodia', 13120000,
10000, 'NA', 2], ['Cameroon', 15740000, 45000, 'NA', 1] ]
def doubleBreak():
"""finds the lines or first line in which words of the form a-z
are located."""
L = [ "------- --------- -------- --- - --------- ---------", # line 0
"------- ---------- a----z -------- -- --------- --------", # line 1
"------- ---------- ------ -------- -- --------- --------", # line 2
"------- ---------- ------ -------- -- --------- --------", # line 3
"------- ---------- ------ -------- az --------- --------", # line 4
"------- ---------- ------ -------- -- --------- --------" ] # line 5
newL = []
for i, line in enumerate( L ):
words = line.split( )
for word in words:
if word[0] == 'a' and word[-1]=='z':
newL.append( i )
print( "the indexes of lines containing a-z words = ", newL )
foundWord = False
for i, line in enumerate( L ):
words = line.split( )
for word in words:
if word[0] == 'a' and word[-1]=='z':
foundWord = True
break
if foundWord:
break
if foundWord:
print( "the indexes of FIRST line containing a-z words = ", i )
else:
print( "Couldn't find an a-z word" )
def getTemperature( zip ):
"""gets the Web page from the weather.com site for the zip code provided
in zip. Returns the number of degrees associated with the "Field Like" box
on the Web page."""
url = BASEURL + zip
print( url )
f = urllib.request.urlopen( url )
bytes = f.read()
htmlText = bytes.decode( "utf8" )
# print( htmlText )
# define the strings we are looking for
front = "Feels Like: <strong>"
back = "°"
# get indexes to the position of 2 strings in htmlText
index1 =htmlText.find( front )
if index1 == -1:
print( "Could not find temperature... Aborting." )
return None
index2 = htmlText.find( back, index1 )
if index2 == -1:
print( "Could not find temperature... Aborting." )
return None
temperature = htmlText[index1 + len( front ): index2 ]
return temperature
def main():
# Exception Part 1
print( "\n\nPart 1\n\n" )
totalPop = 0
totalUsers = 0
for country, pop, users, active, isp in statistics1:
#print( "%30s (%d habitants)" % (country, pop ), end="" )
#print( "Users=", users, " Active=", active, " ISP=", isp )
totalPop += pop
try:
totalUsers += users
except TypeError:
pass
print( totalPop )
print( totalUsers )
print( "\n\nPart 2\n\n" )
# Exception Part 2
totalPop = 0
totalUsers = 0
for countryList in statistics1:
try:
country, pop, users, active, isp = countryList
except NameError:
country, pop, users, active = countryList
totalPop += pop
try:
totalUsers += users
except TypeError:
pass
print( totalPop )
print( totalUsers )
# breaking out of double loops (nested loops)
print( "\n\nPart 3: double break\n\n" )
doubleBreak()
# get the temperature at a given zip code
print( "\n\nPart 4: Web page access\n\n" )
zip = input( "zip? " )
temperature = getTemperature( zip )
if temperature != None:
print( "The temperature is ", temperature , "degrees in zip code", zip )
main()