Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading