[Courses] [python] Lesson 4: Modules and command-line arguments
leslie.brothers at verizon.net
Sat Jul 9 21:30:18 UTC 2011
Help! Regarding exercise 4, my "problem" is that the results for wc -w
and for the program I wrote to count words in a file, come out the same.
I wanted to try the debugging part of the exercise, so I used different
kinds of files (some of the python programs I wrote for previous
questions; some text files with spaces, tabs, multiple lines and so on),
to try to make the results come out different and still the results come
out the same.
Is there a file that is "guaranteed" to give me a different result using
our python techniques from this lesson, and wc -w, and if so can someone
send it to me?
It would be very weird if I accidentally hit on the one way to get a
bullet-proof result with my program -- I'm not that good at programming!
But here's my program:
file = open(sys.argv)
for line in file :
x = len(line.split())
y = y + x
This will probably be the only time in my life when I complain about NOT
having something to debug.
I must have missed something, somewhere.
> 4. Here's a harder problem, an exercise in debugging (which is a big
> part of programming, sadly):
> a. Write a program that counts words in a file (or multiple files,
> if you prefer). Use the same split() and len() you used in
> lesson 2.
> b. Compare the number of words from your program to what wc -w gives.
> (If you're on a platform that doesn't have wc, run it on a small
> file and count by hand.) Are the answers the same?
> c. Here's the debugging part: why aren't they the same?
> (You don't have to fix it: just figure out the problem.)
> Hint: if you're splitting each line into a list, try printing
> the list to see what's in it. In python, if you have a list
> called words, you can just say print words -- you don't have to
> do anything fancy like you would in some languages.
> d. OPTIONAL, harder: fix the problems and make your word count
> program give the same answer as wc -w.
> Hint 1: one Python function that will come in handy is strip():
> it strips off any leading and trailing spaces. So if you have
> a string s = " hello, world ", then s.strip() would
> give you "hello, world".
> By the way, I haven't mentioned Python's documentation, but
> most Python modules have excellent online docs. Here's strip():
> Hint 2: If you're inside a loop, say, looping over lines, and
> you decide you don't care about this line, you can skip to the
> next one by saying:
> For instance, in a loop where you don't care about negative numbers:
> for i in list_of_numbers :
> if i < 0 :
> You can break out of a loop completely with: break
> Don't drive yourself too crazy trying to get an exact match
> with wc. There are some special cases where splitting at spaces
> might not give the same answer as wc -w, and there are some
> other Python modules (specifically re, regular expressions)
> that can do a better job. The purpose of this exercise is to
> give you a taste of debugging, fixing problems as you find them
> and thinking about what special cases might arise.
> Courses mailing list
> Courses at linuxchix.org
More information about the Courses