CSC111 Lab 13 2015
--D. Thiebaut (talk) 09:59, 27 April 2015 (EDT)
<showafterdate after="20150429 13:00" before="20150601 00:00">
This lab is a preparation for the Final Exam. You will notice that the amount of explanations is limited, and you are asked to start from scratch writing a program. The lab will start with a general discussion about approaching the problems, and getting started with such assignments.
Feel free to work in pair programming mode on this assignment.
Problem 1
Write a graphics program that uses a white background, and makes a fish appear wherever the user clicks the mouse.
Once the fish appears on the screen it moves slowly to one side and disappears. While the fish is moving, the user may click on the screen, and make other fish appear. The new fish will then move at the same pace as the first fish, assuming it is still visible.
The user must click in the top left corner of the screen to make the program stop. For example, clicking in area that is 20 by 20 pixels at the top left of the screen will make the program stop.
- Useful Links
- graphics.py
- Documentation on graphics.py
- Page with fish and tank pictures (You do not need the tank for this assignment.)
Problem 2
The file at URL http://cs.smith.edu/dftwiki/media/1000movies.txt contains the list of the best 1000 movies of all times, according to The New York Times.
Write a program that will:
- Ask the user for the year she was born in, e.g. 1995,
- Read the contents of the file at the URL specified above
- Find all the movies that were released in the year specified by the user
- Output the movies sorted by alphabetical order. The year (or parentheses around the year) should not appear in the lines listing the movie titles.
- Save the list to a file called movies1995.txt (replace 1995 by whatever number the user specified as year).
There is nothing to submit to Moodle this week. Just concentrate on making sure you get working versions of the solution programs for this lab.
</showafterdate>
<showafterdate after="20150501 11:00" before="20150601 00:00">
Solution Programs
Program 1
import urllib.request # the lib that handles the url stuff
url = "http://cs.smith.edu/dftwiki/media/1000movies.txt"
def getText( url ):
"""gets the contents of the text file at the given URL"""
# fetch the file and put its contents in the
# string text.
response = urllib.request.urlopen( url )
text = response.read().decode( 'UTF-8' )
return text
def saveToFile( fileName, text ):
"""save the text into the given file """
file = open( fileName, "w" )
file.write( text + "\n" )
file.close()
def main():
"""retrieve the file at the given URL, gets a year
from the user, and outputs all the lines containing that
given year. The output is also saved to a file."""
text = getText( url )
year = input( "Year you were born? " ).strip()
list = []
for line in text.split( "\n" ):
if line.find( year ) != -1:
index = line.find( "(" + year )
line = line[0:index].strip()
print( line )
list.append( line )
saveToFile( "movies"+year+".txt", "\n".join( list ) )
main()
Program 2