Count the input word in a List in Python

enter image description here
Why my code is not working properly ? It’s not counting the word correctly.

Note: The code doesn’t needs to be used for.

    list1 = input().split(sep=',')
    aim_count = list1.count('Enter aimed word to be counted: ')
    print('The count of aimed word: ', aim_count)

>Solution :

You are splitting your sentence by , but in your input, there is no ,

Try this:

list1 = input().split()
aim_count = len(list1)
print('The count of aimed word: ', aim_count)

Note:
by default .split() method splits the sentence by space.

EDIT:
If you want to count the no of a specific word.

try this:

list1 = input().split()

aim word = 'frog'

aim_count = 0
for word in list1:
    if word == aim_word:
        aim_count += 1


print('The count of aimed word: ', aim_count)

Leave a Reply