Due: Wednesday 21 February.
The reading for this assignment is Chapters 9-10 of How to think....
http://gutenberg.netGo to PG and download your favorite of the available books.
import sys def main(script, filename): fp = open(filename) for line in fp: print line if __name__ == '__main__': print sys.argvThe sys module contains functions and variables that pertain to the operating system, including sys.argv, which is a list that contains the arguments you provide on the command line.
If you run the program like this:
python analyze.py gatsby.txtYou should get a list like this:
['analyze.py', 'gatsby.txt']Of course, instead of gatsby.txt you should use the name of the file where you stored your favorite book.
Now remove the line that says print sys.argv and replace it with main(*sys.argv); in other words, invoke main and use sys.argv as the list of arguments. I'll explain what the * means in class.
If you run the program again, it should print the contents of the file. Notice that PG includes some information at the beginning and end that is not part of the book. Edit the file and remove the extra stuff.
That's pretty much all you need to know to get started, but if it helps, here is a list of suggestions for incremental development of the program. You don't have to do all of them. If you are feeling more confident, you can skip steps, but if you end up in debugging heck, you might want to retreat.
The fundamental principles of incremental development are:
The best thing about incremental development is that it minimizes the time you spend debugging. The challenging thing is that it is not always easy to imagine a series of small steps that goes from the starting place to the destination.
Here are some suggestions for steps you might want to follow:
$ head -100 gatsby.txt > short.txt # get the first 100 lines
Hint: if you want to write a global variable inside a function, you have to declare the variable global:
x = 7 def f(): global x # when I say x, I mean the global x x = x + 1
Even if you don't do the JFFEs that follow, you might want to think about them, because they will give you ideas for how you might improve your code.
WHAT TO TURN IN: As always, you should make your printed code as presentable as possible before you turn it in.
You should hand in a copy of analyze.py. Please write on the page, or include in the comments, the name of the book you analyzed and the results (total number of words, and number of different words).