Answer:
myString = input("Please enter a string")
wordList = myString.split()
numofWords = len(wordList)
print('The number of words in {} is {}'.format(myString, numofWords))
Explanation:
Since the suggestion to use a boolean variable flag is not compulsory, I have implemented it in another way.
Using python programming language, the input function is used to prompt the user to enter a string which is saved in a variable myString.
The string entered is converted into a list of all the words by spliting at the blank spaces and saved in the variable wordList
Next we count the number of words by obtaining the len of the list and output to the user