How to count the number of words in the list, provided there is more than one?

  python, python-3.x

For example I have a text,

text = '''
Wales’ greatest moment. Lille is so close to the Belgian border, this was 
essentially a home game for one of the tournament favourites. Their confident supporters mingled 
with their new Welsh fans on the streets, buying into the carnival spirit - perhaps 
more relaxed than some might have been before a quarter-final because they 
thought this was their time.
In the driving rain, Wales produced the best performance in their history to carry 
the nation into uncharted territory. Nobody could quite believe it.
'''

I need to get the number of words in this text, we enter the words with input().
Type will be a list, dict, set this required condition.
It is also not clear how to remove the attention to punctuation marks.
My solution, but perhaps there is a cleaner way.

text = list(text.split(' '))
word = input('Enter a word: ')
for i in text:
    if text.count(word) < 2:
        break
    if word in text:
        print(f'{word} - {text.count(word)}')
        break

Output:

this – 2

the – 7

The ‘moment’ occurs only once in the text, we do not deduce it

Source: Python-3x Questions

LEAVE A COMMENT