Hello, I'm trying to get my program running but I ran into an error. When I want to type a block of text on a new line, I get this error below:
AttributeError: 'str' object has no attribute 'append'
What am I suppose to adjust in order to fix it? The error occurs on the line right before the Text Analyzer Menu shows up.
#Prompt the user to enter a block of text.
done = False
print("Enter as much text as you like. Type EOF on a separate line to finish.")
textInput = ""
while(done == False):
nextInput= input()
if nextInput== "EOF":
break
else:
#The Error occurs here.
textInput.append(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())
#If the user selects Option 1, print out the shortest word found in the
#text.
if option == 1:
print("Shortest word: " + (min(textInput.split(), key = len)))
#If the user selects Option 2, print out the longest word found in the
#text.
elif option == 2:
print("Longest word: " + max(textInput.split(), key = len))
#If the user selects Option 3,
elif option == 3:
word_counter = {}
for word in textInput.split():
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
highest_words = ""
highest_value = 0
for k,v in word_counter.items():
if v > highest_value:
highest_words = []
highest_words.append(k)
highest_value = v
elif v == highest_value:
highest_words.append(k)
for word in highest_words:
print("The word that showed up the most was", highest_words)
#If the user selects Option 4, extract the first letter of the first word
#on each line and merge into s single string.
elif option == 4:
print(''.join(l[0] for l in textInput))
#If the user selects Option 5, grab every 5th word in the text and merge
#into a single sentence.
elif option == 5:
five = ' '.join(word for i,word in enumerate(textInput.split()) if not i%5)
print(repr(five))
#If the user selects Option 6, print out the total number of words in the
#text.
elif option == 6:
print("Total word count:", len(textInput.split(" ")))
#If the user selects Option 7, kill the program.
elif option == 7:
print("You have ended this program. GOODBYE!")
break
#Execute if the user enters a number that's not between 1 and 7.
else:
print("Incorrect option number... try again...")