句子中的单词平均值?

时间:2014-11-04 23:45:26

标签: python list split

print 'The average amount of words in the sentence is' ,average, 'words'

我不能让这个工作

1 个答案:

答案 0 :(得分: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'