Word count from file help.
bignose-hates-spam at and-benfinney-does-too.id.au
Thu Feb 12 02:11:03 CET 2004
On Thu, 12 Feb 2004 01:04:20 GMT, jester.dev wrote:
> I'm learning Python from Python Bible
Welcome, I hope you're enjoying learning the language.
> problems with this code below. When I run it, I get nothing.
More information required:
How are you invoking it (what command do you type)? Does the program
appear to do something, then exit?
You've told us what you expect the program to do (thanks!):
> It should open the file poem.txt (which exists in the current
> directory) and count number of times any given word appears in the
When you encounter unexpected behaviour in a complex piece of code, it's
best to test some assumptions.
What happens when the file "poem.txt" is not there? (Rename the file to
a different name.) This will tell you whether the program is even
attempting to read the file.
What happens when you import this into the interactive Python prompt,
then call CountWords on some text? This will tell you whether the
function is performing as expected.
And so on.
One possible problem that may be a mistake in the way you pasted the
text into your newsgroup message:
> import string
> def CountWords(Text):
> for CharacterIndex in range(0,len(Text)):
> if (__name__=="__main__"):
Indentation defines structural language blocks in Python. The "def",
"for", "if" structures above will encompass *all* lines below them until
the next line at their own indentation level or less.
In other words, if the code looks the way you've pasted it here, the
"def" encompasses everything below it; the "for" encompasses everything
below it; and the "if(PiecesOfWords...):" encompasses everything below
it. Including the "if( __name__ == "__main__" ):" line.
Thus, as you've posted it here, the file imports the string module,
defines a function -- then does nothing with it.
Please be sure to paste the text literally in messages; or, if you've
pasted the text exactly as it is in the program, learn how Python
\ "You've got the brain of a four-year-old boy, and I'll bet he |
`\ was glad to get rid of it." -- Groucho Marx |
Ben Finney <http://bignose.squidly.org/>
More information about the Python-list