[Courses] [python] Lesson 4: Modules and command-line arguments
Akkana Peck
akkana at shallowsky.com
Sun Jul 10 04:29:03 UTC 2011
Leslie writes:
> 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.
[ ... ]
> 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.
Fun! I've been trying to decide whether to hold off for a while
on replying to see what answers other people got ... but I'm going
to go ahead and reply (so this might be a bit of a spoiler for
people who haven't written their word count program yet).
Leslie, here's why your program agrees with wc -w, but I didn't
think most people's would: Leslie, you're using len(line.split()).
In Lesson 3 I talked about splitting by specific characters, e.g.
using line.split(' ') to split at spaces; but I didn't mention (and
probably should have) that you could call split() without specifying
any character. If you don't specify a character, and just say
split(), Python still splits on whitespace, but it does something
different from what it does for split(' ').
What it does is different in two ways, and they're the two things
I had in mind for people to track down in the debugging exercise.
Which seems a bit artificial now since you went straight to a
better solution. :-)
I think I'll shut up at this point and let you figure out what the
differences are. Leslie, you've already written a program that works
fine, so you don't have any errors to debug. But it might be useful
to play around with line.split() versus line.split(' '), see how
they differ and how the difference would affect the final word count.
And if you're motivated to do more coding, think about how you might
implement it if you didn't have split() and had to use split(' ').
...Akkana
More information about the Courses
mailing list