Q:
Here’s something to stop you from getting repetitive when writing essays. Write a program that reads multiple lines of plain text from the user, then prints out each different word in the input with a count of how many times that word occurs. Don’t worry about punctuation or case – the input will just be words, all in lower case. Your output should list the words in alphabetical order.
For example:
Enter line: which witch
Enter line: is which
Enter line:
is 1
which 2
witch 1
A??:
dic_line1 = {}
dic_line2 = {}
list = []
enter_line = input("Enter line: ").lower()
while enter_line:
list_line = enter_line.split()
for n in list_line:
list.append(n)
num_line = list.count(n)
dic_line1[n] = num_line
dic_line2.update(dic_line1)
enter_line = input("Enter line: ").lower()
for word in sorted(dic_line2):
print(word, dic_line2[word])
- lower()
- list.split()
- list.append()
- list.count()
- 按顺序打印字典for n in sorted(dictionary)