CSC111 HW 11
--Thiebaut 21:47, 20 April 2009 (UTC)
This assignment is due on Monday, 4/27/09, at midnight. You may work in pairs on this assignment, in which case you may submit one program only, and include the two names in the header of the program.
Write a program that computes the current value of somebody's portfolio. The user of your program will be prompted to enter the symbol of the companies in which she owns shares, along with the number of shares, and the program will output the total value of her investment.
Here is an example of how your program will work (the user input is underlined):
- Please enter the symbols of each company, followed by the number of shares:
- ? IBM 1000
- ? CSCO 2000
- ? MSFT 1000
- The total value of your investment in IBM, CSCO, MSFT is $154000
The program went to the URL of Yahoo Finance, at http://finance.yahoo.com/q?s=CSCO (for the CSCO symbol), and retrieved the real-time quote for IBM, Cisco, and Microsoft. The figure shows that the quote for Cisco at the time this was written was $17.55.
- Call your program hw11b.py
- Your program should use a loop to get the input from the user. The loop will stop automatically (break) when the user enters an empty line. We assume that the user will never have more than 100 different companies to list.
- Your program should assume that the symbol of the company is always listed first, and that the number of shares is always second on the line, separated from the symbol by white space. This number can be an integer or a float. If your program expects the information in a different manner, the program that tests the submitted assignment will not be able to correctly feed data to your program, and it will look like your program is not working. So make sure you follow carefully the requirement for the input.
- Your program should get the information from http://finance.yahoo.com
- Your program should use the getUrlInfo.py module.
- Your program should be fully documented, with a main header, a header for each function, and documentation at key parts of the code.
Be careful; you will find that the string "yfs_l90" is an important part of finding the information of interest here. The second part of the string starts with an ell, not a one!
For your information: You can find the list of NASDAQ symbols here.
submit hw11 hw11b.py submit hw11 getUrlInfo.py
For this problem, you have to compute the average current temperature of Massachusetts by picking 10 Massachusetts towns at random, figuring out the current temperature in each one of them, and computing the average of the 10 temperatures.
- Program name: call your program hw11a.py
- Finding the towns and their zip codes. The URL http://www.mongabay.com/igapo/zip_codes/MA.htm
contains a list of Mass towns with their zip code. Make your program get the list of all the towns from this URL.
- Picking 10 towns at random from the list: Section 10.4.2 of the text book covers this very nicely. Use the same method.
- Output. The output of your program should contain the name of the 10 towns, their zip code, the temperature in each one, and the overall temperature. Something like this would work:
- 01026 Cummington 54 degrees
- 01032 Goshen 52 degrees
- 01037 Hardwick 53 degrees
- 01041 Holyoke 45 degrees
- 01063 Northampton 44 degrees
- 01075 South Hadley 44 degrees
- 01082 Ware 53 degrees
- 01083 Warren 39 degrees
- 01088 West Hatfield 52 degrees
- 01094 Wheelwright 51 degrees
- Average Massachusetts temperature: 48.7 degrees
- The average temperature should be a float.
- The program will not ask the user for any information (and neither will the program we will use to test all the programs submitted, so do not make your program prompt the user for any information, please!)
- Your program should use the module getUrlInfo.py you have been using in Lab 11.
Submit both your program and your version of getUrlInfo.py, which you are allowed to modify. If you modify it, please indicate it in the header of hw11a.py. Thanks!
submit hw11 hw11a.py submit hw11 getUrlInfo.py
Make sure you document your program well. Check the recent solution programs for past homework assignments as guidance.
Selected solution programs for Lab #11
# warning: this program is totally undocumented # (on purpose :-) import getUrlInfo towns = """ 02351 Abington 02018 Accord 01720 Acton 02743 Acushnet 01220 Adams 01001 Agawam 02134 Allston 01913 Amesbury 01002 Amherst """ def main(): #--- create an object of type getUrlInfo --- tempSite = getUrlInfo.getUrlInfo() #--- initialize it --- tempSite.setUrl( "http://www.weather.com/outlook/" +"travel/businesstraveler/local/%s" ) tempSite.setMarkers( "\"obsTempTextA\">", "°" ) sumTemp = 0 count = 0 townNames =  for line in towns.split( "\n" ): words = line.split() if len( words )==2: zip, town = words temp = tempSite.getInfo( zip ) if temp is not None: print "temperature in %s is %s" % ( town, temp ) main()
# warning: this program is totally undocumented # (on purpose :-) import getUrlInfo def main(): dirSite = getUrlInfo.getUrlInfo() dirSite.setUrl( "http://www.smith.edu/global_campusdirectory.php" +"?name=%s" ) # html section of interest: # <br>(413)585-4222<br><a href="mailto:firstname.lastname@example.org?subject dirSite.setMarkers( "br>(413)", "<br>" ) phone = dirSite.getInfo( "cardell" ) dirSite.setMarkers( "<a href=\"mailto:", "?subject" ) email = dirSite.getInfo( "cardell" ) print "phone = %s email = %s " % ( phone, email ) main()