来自美国的字数

2024-07-08 08:07:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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, "")

Tags: of代码用户程序you编码asline
1条回答
网友
1楼 · 发布于 2024-07-08 08:07:08

您应该将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。你知道吗

相关问题 更多 >

    热门问题