大家好,我是一个编程初学者,我最近被赋予了创建这个程序的任务,我发现它很难。我以前设计过一个程序,计算用户输入的句子中的单词数,是否可以修改这个程序来实现我想要的目标?
import string
def main():
print "This program calculates the number of words in a sentence"
print
p = raw_input("Enter a sentence: ")
words = string.split(p)
wordCount = len(words)
print "The total word count is:", wordCount
main()
使用^{} 计算字数,使用open()打开文件:
collections.Counter
示例:Counter.most_common根据字数按排序顺序返回单词:
要打开文件,可以使用open函数
相关问题 更多 >
编程相关推荐