This is a modified program from the word count program that I posted about. I am trying to calculate the average word length in a sentence. Here is my code so far:

def main():
    print "This program will calculate the average word length in a sentence"
    print
    s = raw_input("Enter a sentence: ")
    words = string.split(s)
    wordCount = len(words)
    ch = string.split(words)
    charCount = len(ch)
    avgLength = charCount / wordCount
    print "The average word length of the sentence '", s,"', is", avgLength,"words."
main()

I keep getting an error like 'list' object has no attribute 'split'.

I am a little confused with the 'string.split(s)' I thought it was more like s.split():

s = "Just a test string"
words = s.split()
print words

Are there different versions of Python floating about?

Well, yes ... but they are strictly upgrades, not forks. The 'string.split(s)' is a now-deprecated way of writing 's.split()'

I keep getting an error like 'list' object has no attribute 'split'.

And you really don't know why?

words = string.split(s)

Ok, from your other post you know that words is a list. Fine.

ch = string.split(words)

What are you doing here? You want to split a list? Worse than that, you want to do it with string.split()! ;)

Here is a receipe how to find the average word-length:
1. Split the string into words (hint: We have that already ;))
2. Do something like total = 0 . We need this later
3. Iterate over the words in the list. (hint: for word in words: )
4. Find the length of the current word (hint: a word is just a list of characters ;))
5. Add this length to total.

6. Ok, after we've done this with every word, total is the sum of the wordlengths.
7. Divide total by the number of words.
8. That's it.

Okay, I'll try that. Sorry, I'm a beginner at Python so I might ask stupid or obvious questions. Just bare with me. It'll come easier to me eventually.

ok, got it:

def main():
    print "This program will calculate the average word length in a sentence"
    print
    s = raw_input("Enter a sentence: ")
    words = string.split(s)
    wordCount = len(words)
    sum = 0
    for word in words:
        ch = len(word)
        sum = sum + ch
    avg = sum / wordCount
    if avg is 1:
        print "In the sentence '", s,"', the average word length is", avg,"letter."
    else:
        print "In the sentence '", s,"', the average word length is", avg,"letters."
main()

Okay, I'll try that. Sorry, I'm a beginner at Python so I might ask stupid or obvious questions. Just bare with me. It'll come easier to me eventually.

If my posts sound somewhat rude, I'm sorry for that. It's not meant this way.
Your questions are absolutely not stupid, and before you ask you try to find a solution by yourself. That's more than some others here do.

Ok, just one comment on your code:

sum = 0

There is a builtin-function called sum(). It's bad style to call variables like builtins, and can lead to very confusing errors.

Here's a shorter version of your code(using sum):

s = "This is a short sentence"
l = s.split()
print sum( [ len(word) for word in l ] ) / len(l)

Regards, mawe

Good job! Just to make your code robust, you should check for dividing by zero:

if wordCount != 0:
    avg = sum / wordCount
else:
    avg = 0

If you want to continue to improve the code beyond that, you might consider turning the average word count into a separate function, so that it could be used by other programs for any old purpose. Doing so would have the positive side effect of cleaning up your code.

In general, it's usually better to have lots of small functions that do one thing rather than a few number of large functions that do lots of things. Your main() interacts with the user, finds the average, and prints the results ... a bit too Swiss-army-knifish for my taste. :)

Also, you might consider reading the Python docs tutorial on list generators. A list generator can simplfiy your code like this:

total = sum([len(word) for word in words]

That one line replaces the entire sum=0 and for-loop section.

Jeff

split will break if your input have punctuations. eg word.Test . This will be counted as 1?
(although a blank space comes after a full stop.)
anyway, here's another way to do it,

>>> import re
>>> s = "This program will calculate the average word length in a sentence.Tehas dfsdskj 323 5543"
>>> len(re.findall("\w+",s))
15
This question has already been answered. Start a new discussion instead.