96.0k views
3 votes
Write a function called word_count. word_count takes as #input a string called my_string, and returns as output the #number of words in the string. For the purposes of this #problem, you can assume that every space indicates a new #word; so, the number of words should be one more than the #number of spaces. You may also assume that any strings are #not empty, so there should always be at least one word if #my_string is a string. #

User Bricks
by
4.8k points

1 Answer

7 votes

Answer:

def word_count(string):

tokens = string.split()

n_tokens = len(tokens)

return n_tokens # <-- here is the difference

print(word_count("Hello World!"))

print(word_count("The quick brown fox jumped over the lazy dog."))

Step-by-step explanation:

User Monkeyjumps
by
4.8k points