一句话中的平均词数?
print 'The average amount of words in the sentence is' ,average, 'words'
我就是搞不定这个。
1 个回答
0
你应该把每个单独的字符加起来,然后除以你列表中的单词数量。
单词是通过空格分开的(不过你要考虑不同的情况)。nbrChar
是一个用来计算所有单词中字符数量的变量。
from __future__ import division
sentence = raw_input ("Please write a sentence for it to be counted and averaged:")
words = sentence.split(" ") #You should split words by space
nbrChar, average=0, .0
for word in words:
nbrChar+= len(word)
average= nbrChar // len(words)
print 'The average amount of words in the sentence is' ,average, 'words'