用python代码实现使用input函数输入一篇英文文章,统计该文章中各单词出现的次数并输出。
时间: 2024-02-29 17:51:50 浏览: 34
好的,以下是实现代码:
```python
# 输入文章
article = input("请输入一篇英文文章:")
# 将文章转换为小写并去除标点符号
article = article.lower().replace(",", "").replace(".", "").replace("?", "").replace("!", "")
# 分割单词
words = article.split()
# 统计单词出现次数
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 输出结果
for word, count in word_count.items():
print(word, count)
```
以上代码中,我们首先使用 `input` 函数获取用户输入的英文文章,然后将其转换为小写并去除标点符号。接着,我们使用 `split` 函数将文章分割成单词,并使用字典 `word_count` 统计每个单词出现的次数。最后,我们遍历 `word_count` 字典,输出每个单词及其出现次数。