来自用户的字数

时间:2017-11-11 01:29:34

标签: python python-3.x

我对Python和编码一般都是新手。我需要编写一个允许用户输入多行的代码,当他们写完多个句子后,他们进入一个句点来停止程序,然后程序会告诉用户输入了多少个单词。我该怎么做呢?

这是我到目前为止所做的:

print("Enter as many lines of text as you want.")
print("When you're done, enter a single period on a line by itself.")

while True:
    print("> ", end="")
    line = input()
    if line == ".":
        break
    totalWords = line.split()
    newWords = totalWords.append(line)
wordCount = len(newWords)
print("The number of words entered:" , wordCount, "")

1 个答案:

答案 0 :(得分:0)

您应该将content设置为循环。

content = []
while True:
    line = input()
    if line == ".":
        break
    words = line.split()
    content.append(words)

words_list = [item for sublist in content for item in sublist]
print(len(words_list))

此外,大多数更改序列/映射项的函数都会返回None。因此newWords = totalWords.append(line)将始终返回None