Word count from file help.

jester.dev jester.dev at comcast.net
Thu Feb 12 18:46:54 EST 2004


See inline.

Ben Finney wrote:

> On Thu, 12 Feb 2004 01:04:20 GMT, jester.dev wrote:
>> I'm learning Python from Python Bible
> 

> How are you invoking it (what command do you type)?  Does the program
> appear to do something, then exit?
> 

I made it executable: chmod 755 word_count.py
I also tried: python word_count.py


> Diagnostics:
> 
> 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.

It does nothing either way. First time I ran it the file was not there. 
 
> 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.

Nothing happens. :) So I guess what you said below is correct.

> And so on.
> 
> 
> One possible problem that may be a mistake in the way you pasted the
> text into your newsgroup message:
> 
>> #!/usr/bin/python
>> [...]
>> import string
>>     
>> def CountWords(Text):
>> [...]
>>         for CharacterIndex in range(0,len(Text)):
>> [...]
>>                 if(PiecesOfWords.find(CurrentCharacter)!=-1):
>> [...]
>>                 else:
>>                                 if(CurrentWord!=""):
>> [...]
>>                                 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
> interprets indentation:
> 
>     <http://www.python.org/doc/current/ref/indentation.html>
> 

Thanks for the link. I'm not really used to this whole indention deal yet. I
as however using WingIDE which indents for me.

JesterDev



More information about the Python-list mailing list