텍스트 파일에서 가장 많이 나타나는 단어를보고하도록 프로그램을 설정하려고합니다. 예를 들어, "Hello like I like pie"라고 입력하면 프로그램이 "가장 많이 발생한 것"을 인쇄해야합니다. KeyError를 : 옵션 3을 실행했을 때이 오류가 'H'Python : 가장 많이 나타나는 단어 찾기?
#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)
나는 여전히 .split() – user2581724
난 그냥 실현을 통합 한 후 키 오류, 내 업데이트 된 대답을 살펴 있습니다. – Th3Cuber
자, 이제 프로그램은 입력 단계에서 입력 한 마지막 단어 만 인쇄합니다. 내가 "그 사람이 너무 닮은 것 같아"라고 말하면, 단어의 뜻이 나타나고 "좋아"하지 않습니다. 무엇이 잘못 될 수 있습니까? – user2581724