In python please, and i need in it in a simple way (Comment the
code so i can understand the logic)
Write a function called mostfrequent that takes one string
argument containing a
list of words separated by commas (e.g., “apple,banana,apple,pear”,
note there
are no spaces). The function must return the word that occurs most
frequently in
the input string. You can assume there will be no ties in
frequency.
Examples:
mostfrequent(“apple,apple,banana,apple,peach,banana”)
“apple”
mostfrequent(“apple,banana,banana,apple,peach,banana”)
“banana”
mostfrequent(“apple”) “apple”
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.split():
if word in textInput:
word_counter[word] += 1
else:
word_counter[word] = 1
highest_words = []
highest_value = 0
for k,v in word_counter.items():
# if we find a new value, create a new list,
# add the entry and update the highest value
if v > highest_value:
highest_words = []
highest_words.append(k)
highest_value = v
# else if the value is the same, add it
elif v == highest_value:
highest_words.append(k)
print("The word that showed up the most was: ", word)
In python please, and i need in it in a simple way (Comment the code so...