Ask Question

Write a function called word_count that takes a single argument, a string containing the text of a single chapter, and returns the number of words in that chapter. Assume that words are separated from each other by spaces. Apply your function to compute the variable wc (defined in the cell below).

+3
Answers (1)
  1. 13 March, 22:08
    0
    Hi there! There are a number of ways to achieve the word count result given an input text. The easiest method is explained below.

    Explanation:

    A simple Python script, word_count. py, can be used to calculate the word count program. The first argument is taken as the input text string (it needs to be enclosed in quotes (" ") in the command line). This input text is then split by spaces to capture the complete words list in an array. Finally, the length of the array equals the word count.

    word_count. py

    import sys;

    text = sys. argv[1];

    words = text. split (" ");

    wc = len (words);

    print (wc);
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a function called word_count that takes a single argument, a string containing the text of a single chapter, and returns the number ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers