我试图让我的程序报告文本文件中出现最多的单词。例如,如果我输入“你好,我喜欢馅饼,因为它们太棒了”,程序应该打印出“喜欢的次数最多”。执行选项 3 时出现此错误:KeyError: 'h'
#Prompt the user to enter a block of text.
done = False
textInput = ""
while(done == False):
nextInput= input()
if nextInput== "EOF":
break
else:
textInput += nextInput
#Prompt the user to select an option from the Text Analyzer Menu.
print("Welcome to the Text Analyzer Menu! Select an option by typing a number"
"\n1. shortest word"
"\n2. longest word"
"\n3. most common word"
"\n4. left-column secret message!"
"\n5. fifth-words secret message!"
"\n6. word count"
"\n7. quit")
#Set option to 0.
option = 0
#Use the 'while' to keep looping until the user types in Option 7.
while option !=7:
option = int(input())
#The error occurs in this specific section of the code.
#If the user selects Option 3,
elif option == 3:
word_counter = {}
for word in textInput:
if word in textInput:
word_counter[word] += 1
else:
word_counter[word] = 1
print("The word that showed up the most was: ", word)