We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,389 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

# 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.
main()
3
Contributors
2
Replies
14 Hours
Discussion Span
5 Months Ago
Last Updated
3
Views
elbert.leach
Newbie Poster
1 post since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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

Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0605 seconds using 2.67MB