для этой программы. Я пытаюсь попросить пользователя ввести столько текста, сколько он хочет в файле, и программа подсчитывает общее количество слов, которые были сохранены в этом файле. Например, если я напечатаю «Привет, мне нравится есть черничный пирог», программа должна читать в общей сложности 7 слов. Программа работает нормально, пока я не набираю вариант 6, где он подсчитывает количество слов. Я всегда получаю эту ошибку: «ул» объект имеет атрибута «предметов»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())
#I get the error in this section of the code.
#If the user selects Option 6, print out the total number of words in the
#text.
elif option == 6:
count = {}
for i in textInput:
if i in count:
count[i] += 1
else:
count[i] = 1
#The error lies in the for loop below.
for word, times in textInput.items():
print(word , times)
Благодарим вас за помощь! – user2581724
Кстати, '.split()' без аргументов удаляет любой символ пробела, добавляя новые строки и вкладки. '.split ('')' будет удалять только слова, разделенные пробелом. – OdraEncoded