''' Purpose: examine a list of words ''' # get text from user reply = input( 'Enter text: ' ) print() # split text into words - words is a list of things that include the words in the string reply where each word is separated by a space words = reply.split() # let's see what we got print( 'Supplied text =', reply ) print( 'words =', words ) print() # determine number of words - how many words are in the list words? n = len( words ) # the len( x ) function can take in a string OR a LIST # x = string --> finds how many characters there are in x # x = list --> how many elements there are in the list x # if we had used len( reply ), it would tell us how many characters are in reply, not how many words there are # len() returns an int # REVIEW THIS CONCEPT! THIS IS VERY IMPORTANT!!!!!!! # determine total word length sum_of_word_lengths = 0 # at the beginning, the accumulator starts at 0 for a summation problem because we haven't added anything yet # this is an int print( 'sum_of_word_lengths:', sum_of_word_lengths ) # for each word in our words list, let's figure out the length of the current word --> gives you an int # then we update the accumulator by adding the length of the current word to the RUNNING total # when the loop finishes, we have the final value of the sum for w in words : # get current word's length # our current word is represented by the variable w # need an assignment statement so we can recall the value later current_length = len( w ) # len() returns an int # print( w, ':', current_length ) # use this line to check if you're on the right path # add length to the accummulation # we want the length of each word to be part of the total in sum_of_word_length # --> we update the accumulator, increase the sum by the current length sum_of_word_lengths = sum_of_word_lengths + current_length print( 'sum_of_word_lengths:', sum_of_word_lengths ) # prints the new value of the sum_of_word_length after we have updated it on line 52 # if you print this value before line 52, it will give you the current version before you updated # after the loop, we have found the sum of all the word lengths! # determine average average_word_length = sum_of_word_lengths / n # we can take the final value of the sum of the word lengths and divide by the number of words (n) to get the average # remember one / is decimal division, so we get decimal answer print( 'Average word length:', average_word_length )