【发布时间】:2016-05-18 13:18:36
【问题描述】:
我对 python 还是很陌生,我需要一个程序,它不仅可以计算输入句子中的单词,还可以计算每个单词中的字母数。这就是我到目前为止所拥有的。任何帮助将不胜感激!
def main():
s = input("Please enter your sentence: ")
words = s.split()
wordCount = len(words)
print ("Your word and letter counts are:", wordCount)
main()
【问题讨论】:
-
单句输入是否需要 TotalWordCount 和 TotalLetterCount?
-
两者。我使用 for 循环来获取计数。 s = input("请输入你的句子:") words = s.split() wordCount = len(words) print("你的单词和字母数是:", wordCount) count = 0 for ch in words: count += 1 print(len(ch)) main()
标签: python