编写一程序,允许从键盘输入任意多个英语单词(单词可以重复),中间用空格分开,输入0 表 示输入结束。该程序可以统计同一个英语单词被输入几次,最后对英文单词按字典顺序输出,后面 跟上该单词被输入的次数。
时间: 2024-04-14 09:28:04 浏览: 124
Sure, I can help you with that. Here's a Python program that allows you to input multiple English words from the keyboard, counts the occurrences of each word, and outputs them in alphabetical order along with the frequency:
```python
def count_words():
words = []
while True:
word = input("Enter an English word (or 0 to exit): ")
if word == '0':
break
words.append(word)
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
sorted_words = sorted(word_count.items())
for word, count in sorted_words:
print(f"{word}: {count}")
count_words()
```
You can run this program in any Python environment and follow the instructions to enter the English words. Once you enter '0', the program will display the words along with their respective frequencies in alphabetical order.
阅读全文