CSC111 Lab 11

From CSclasswiki
Jump to: navigation, search

--Thiebaut 20:27, 20 April 2009 (UTC)

Exercise #1: Astrology


Play with the astrology program we saw in class. You can get a copy of it with

  • Run it.
  • Play with it. Try different signs.
  • Test it. Enter a sign in uppercase letters. One in lowercase letters. In mixed case letters.
  • Enter a sign that does not exist.

Verify that it prints the correct information, i.e. the information that appears on the Web page at URL (By the way, URL stands for Uniform Resource Locator).

Keep this program safely tucked away: you will modify it to get the temperature of a location in the next exercise.

Exercise #2: Temperature


You will do the same exercise we did this morning in class, but this time you write a program that gets the current temperature from a location for which you provide the zip-code.

  • Verify that you get the current weather for Amherst.
  • Modify the zip code at the end of the URL in the navigation bar of your browser to find the temperature at other locations in the States (you can find zip codes here).
  • Now make the browser open the source for this page (right click, View Page Source on FireFox, Apple-U on a Mac, right click, View Source on Internet Explorer).
  • Search the source for the words '"Feels Like'" which should take you to the part of the html page that contains the temperature. Locate the temperature which is followed by °.
  • Figure out what string to ask your Python program to search for that is unique enough, and preceding the temperature digits. Let's call it beginSection.
  • Figure out what string to ask your Python program to search for that is unique enough, and trails the temperature digits. Let's call this string endSection
  • Make a copy of the program and call it
  • Edit it and make it behave as follows:
    1. it will ask the user for a zip code
    2. it will include the zip code in the URL of the site (
    3. it will grab the html page from this URL
    4. it will slice the text that appears between the two markers you identified earlier.
    5. it will print the temperature and the zip code in a sentence.

Exercise 3: Smith Directory



Same as Exercise 2. This time the URL is and you get information on a person by appending ?name=person-name at the end of the URL.

For example, if you wanted information on Judy Cardell, you would write the URL as

Try it!


Follow the same steps you took in Exercise 2, but create instead a program called, which asks the user for a name and returns that person's email address.

Exercise 4: Using classes

Get a copy of the module we saw in class this morning:


This program is a module, but it can also be used as a stand-alone program.

Run it as a stand-alone program as follows:


And verify that you get the correct temperature for zip code 01002. This zip code is hard-wired in the program. Modify the program so that it gets the temperature for a different zip code, for example that of Beverly Hills, CA, zip code 90210.

Creating a new program that uses this module

You will now use as a module that is imported by another program.

Create this new program with emacs, and call it


import getUrlInfo

def main():
    #--- create an object from the clas getUrlInfo ---
    tempSite = getUrlInfo.getUrlInfo()

    #--- initialize it ---
    tempSite.setUrl( ""
                     +"travel/businesstraveler/local/%s" )
    tempSite.setMarkers( "obsTempTextA\">", "°" )

    #--- get the information from the site ---
    zip = raw_input( "Enter zip code: " )
    temperature = tempSite.getInfo( zip )
    print "temperature in %s = %s degrees" % ( zip, temperature )

  • Run the program
  • Verify that it gives the correct information when you feed it different zip codes.

Challenge #1 of the Day

Assume that we want the average temperature in Massachusetts by taking the individual temperatures for the following 9 towns, for which the zip code is indicated on the left-hand side, and by computing the overall average:

       02351 Abington
       02018 Accord
       01720 Acton
       02743 Acushnet
       01220 Adams
       01001 Agawam
       02134 Allston
       01913 Amesbury
       01002 Amherst

Modify your last program so that it computes this average temperature over these 9 towns.


  • Use a loop!
  • Put the whole list above in """ triple quotes """ and split it sections that are useful to you

Exercise 5: Using the class to get the fortune of the day

Use what you did for Exercise 4 and create a new program called that is a clone of, but that asks the user for her sign, and displays her astrological daily horoscope.

Exercise 6: Using the class to get somebody's email address

Same idea as for Exercise 5, but this time you create a new program, called that uses the module, asks the user for a person's name, and returns that person's email address (assuming he/she works at Smith).

Challenge #2 of the Day

Modify your program so that it reports not only the email address of the person, but also his/her phone number.