1,105,592 Community Members

I need help finding the average number of words per sentence, within a list

Member Avatar
Newbie Poster
1 post since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
# This program reads numbers from a file into a list.
def main():
    # Open a file for reading.
    infile = open('text.txt', 'r')
    sentences = infile.readlines() # Read the contents of the file into a list.
    infile.close()                          # Close the file.

    index = 0                               # Convert each element to an int.
    while index < len(sentences):

        print (sentences[index], index) #PRINT EACH ELEMENT
        index += 1

        # Print the contents of the list.

    print (index)

# Call the main function.
Member Avatar
Posting Maven
3,456 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]

Take a page containing a list of sentences, like this one (take only the first 5 sentences). Then with a calculator, compute the average number of words per sentence by hand and note carefully everything you do. This should give you a working algorithm. Write pseudo code, then python code.

Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]

Assume the sentences end with '.' or '?' or '!' so count these characters and divide the total words by the number of these characters.

This article has been dead for over three months: Start a new discussion instead
Start New Discussion
Tags Related to this Article