RE: word count programming for python (wc)

Reply

Join Date: Mar 2008
Posts: 5
Reputation: huglybits is an unknown quantity at this point 
Solved Threads: 0
huglybits huglybits is offline Offline
Newbie Poster

RE: word count programming for python (wc)

 
0
  #1
Mar 13th, 2008
I would like some advice as to how to write my wc program. Program should accept a file name as input and then print three numbers showing the count of lines, words and characters in the file. Chapter 4 question 15 of zelles introduction to python
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: bgeddy is an unknown quantity at this point 
Solved Threads: 1
bgeddy's Avatar
bgeddy bgeddy is offline Offline
Newbie Poster

Re: word count programming for python (wc)

 
0
  #2
Mar 14th, 2008
Assuming the question does not want a count of unique words/characters this should do it:

  1. open the file for reading
  2. number of words,characters, lines = 0
  3. while a line can be read from the file read a line into a string
  4. number of lines + = 1
  5. number of characters + = length of string
  6. number of words + = length of string split into a list of words
  7. print out number of words, lines, characters

Obviously this is not python but pseudo code that will very easily translate into python - I'll leave that to you. Look into string functions for splitting strings into a list of words (hint). The length of this list will give you the number of words. The length of a string is the number of characters and the number of line reads is the number of lines.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: huglybits is an unknown quantity at this point 
Solved Threads: 0
huglybits huglybits is offline Offline
Newbie Poster

Re: word count programming for python (wc)

 
0
  #3
Mar 14th, 2008
Thanks bgeddy. I really do not have much clue as to what I am doing but I am very grateful for your response. Do I type the toggle as written? What am I expected to substitute in the toggle?

Huglybits.
Last edited by huglybits; Mar 14th, 2008 at 5:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,001
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 284
woooee woooee is offline Offline
Veteran Poster

Re: word count programming for python (wc)

 
0
  #4
Mar 14th, 2008
Program should accept a file name as input
Do you want the program to ask the user to key in a file name, or do you want to provide it as an argument when the program is called, or do you want to use a GUI that allows the user to browse the file system and choose a file?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: huglybits is an unknown quantity at this point 
Solved Threads: 0
huglybits huglybits is offline Offline
Newbie Poster

Re: word count programming for python (wc)

 
0
  #5
Mar 14th, 2008
I believe I want to use a GUI that allows the user to browse the file system and choose a file or to ask the user to key in a file name. I got the file which is saved in a .txt file. bgeddy used some + signs in his toggle please tell me what to do. Thanks woooee!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Solved Threads: 38
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: word count programming for python (wc)

 
0
  #6
Mar 15th, 2008
You can use
number = number + 1
or
number += 1
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: bgeddy is an unknown quantity at this point 
Solved Threads: 1
bgeddy's Avatar
bgeddy bgeddy is offline Offline
Newbie Poster

Re: word count programming for python (wc)

 
0
  #7
Mar 15th, 2008
Sorry for not getting back sooner but it seems other kind folks in the forum have answered already.
It's hard to gauge someones level of experience when answering a question but I only outlined the program's logic broadly.

I recommend studying a beginners tutorial on python and playing around in the interpreter, following examples etc to get a feel for things.

Personally, if you are just starting out with programming in general, I wouldn't recommend developing gui front ends just yet. Perhaps just "raw_input() to read input from the user and "print " to output it.

Please don't take offence if I underestimate your level of experience and don't think I'm being deliberately vague in offering a solution. If I were to simply give you python code for a complete working program I don't think you'd learn as much as doing it for yourself. If this is a homework assignment, simply copying an offered solution verbatim will give a false impression of your proficiency.

Good luck with python !
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: huglybits is an unknown quantity at this point 
Solved Threads: 0
huglybits huglybits is offline Offline
Newbie Poster

Re: word count programming for python (wc)

 
0
  #8
Mar 16th, 2008
Thats right! I will try to understand this, it is not all complicated. Thanks for all your help!
Last edited by huglybits; Mar 16th, 2008 at 11:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: misty_mirage is an unknown quantity at this point 
Solved Threads: 0
misty_mirage misty_mirage is offline Offline
Newbie Poster

Re: word count programming for python (wc)

 
0
  #9
Mar 18th, 2008
hi
I tried to the program mentioned above ,but my problem is I can get them done as three separate programs instead of one.Can anyone help me out?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 41
Reputation: rysin is an unknown quantity at this point 
Solved Threads: 0
rysin's Avatar
rysin rysin is offline Offline
Light Poster

Re: word count programming for python (wc)

 
0
  #10
Mar 18th, 2008
I asked this question to someone back when I was coding my text editor(horrible project). THis is what that person gave me to use.

  1. def count_words(self):
  2. text = self.text.get(1.0, 'end')
  3. words = len(text.split())
  4. print words
  5. self.master.title("Text has %d words" % words)
ITs a function that counts words. Hope this helps!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC