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?
Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
Well, yes ... but they are strictly upgrades, not forks. The 'string.split(s)' is a now-deprecated way of writing 's.split()'
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
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
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156