By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Breaking down sentances

P: 16
thanks for your help with my previous problem but i got another one hopefully this doesnt bother you guys at all. how do i go about writing a program that counts the number of words in a sentence by the user and make sure that it calculates the avrage word length of that sentence. thanks once again
May 13 '07 #1
Share this Question
Share on Google+
7 Replies


bartonc
Expert 5K+
P: 6,596
thanks for your help with my previous problem but i got another one hopefully this doesnt bother you guys at all. how do i go about writing a program that counts the number of words in a sentence by the user and make sure that it calculates the avrage word length of that sentence. thanks once again
It's really no trouble. Software development help is what TheScripts.com is all about.

It really helps us help you if you post some code that shows what you able to do and what you are having trouble with. Also, good thread titles help people know what your problem is in general. There are some Posting Guidelines here. Please take some time to read through some of them.

Thanks for seeking help on the Python Forum,
Barton
May 13 '07 #2

Expert 100+
P: 511
thanks for your help with my previous problem but i got another one hopefully this doesnt bother you guys at all. how do i go about writing a program that counts the number of words in a sentence by the user and make sure that it calculates the avrage word length of that sentence. thanks once again
assuming words are separated by spaces(one or more)
here's a rough pseudocode:
Expand|Select|Wrap|Line Numbers
  1. prompt user for sentence # raw_input()
  2. split the sentence by spaces and store somewhere # split() and list storage
  3. count the length of the list # len()
  4.  
however i don't understand what you meant by avg word length, so i leave it to you for clarification.
May 14 '07 #3

P: 16
assuming words are separated by spaces(one or more)
here's a rough pseudocode:
Expand|Select|Wrap|Line Numbers
  1. prompt user for sentence # raw_input()
  2. split the sentence by spaces and store somewhere # split() and list storage
  3. count the length of the list # len()
  4.  
however i don't understand what you meant by avg word length, so i leave it to you for clarification.
avg is the average word lenght in the sentnce so the average of the words in that sentence
May 20 '07 #4

bartonc
Expert 5K+
P: 6,596
Expand|Select|Wrap|Line Numbers
  1. s = "This is a very short sentance. And this is one that\nspans lines."
  2. print s
  3. ##This is a very short sentance. And this is one that
  4. ##spans lines.
  5.  
  6. sentances = s.split(".")
  7. print sentances
  8. ['This is a very short sentance', ' And this is one that\nspans lines', '']
  9.  
  10. nWords = 0
  11. nCharacters = 0
  12.  
  13. for sentance in sentances:
  14.     if sentance:
  15.         words = sentance.split()
  16.         for word in words:
  17.             nWords += 1
  18.             nCharacters += len(word)
  19.  
  20. avgWordLen = nCharacters/float(nWords)
  21.  
  22. print "Number of words = %d, with a total of %d charaters.\nSo the average word length is %.4f"\
  23. %(nWords, nCharacters, avgWordLen)
  24.  
This is a very short sentance. And this is one that
spans lines.
['This is a very short sentance', ' And this is one that\nspans lines', '']
Number of words = 13, with a total of 50 charaters.
So the average word length is 3.8462
May 20 '07 #5

P: 16
thanks for the help you guys its really good i want to also find out what is the best book for python would you reccommend for a someone who is quite new to python.
May 29 '07 #6

bartonc
Expert 5K+
P: 6,596
thanks for the help you guys its really good i want to also find out what is the best book for python would you reccommend for a someone who is quite new to python.
As stated in this thread (found by searching TheScripts for python books),

Learning Python
by Mark Lutz and David Ascher
publisher: O'Reilly
May 29 '07 #7

Expert 100+
P: 511
thanks for the help you guys its really good i want to also find out what is the best book for python would you reccommend for a someone who is quite new to python.
i always recommend the Python doc site. It has many resources you can look at, esp take the tutorial by the Python creator himself. Also once you get the hang, look through the library references etc..
May 29 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.