''' Purpose: examine a list of words ''' # get text from user reply = input( 'Enter text: ' ) print() # split text into words words = reply.split() # A list of words where reply was split at whitespace is stored in words # let's see what we got print( 'Supplied text =', reply ) print( 'words =', words ) print() # analyze text # For loop to process the list of words where we print each word and its length # If reply was 'Welcome to CS' # words is ['Welcome', 'to', 'CS'] # the loop variable w will take on each smaller element from the list of words # first it will be 'Welcome' then execute the statements for that value # second it will be 'to' then execute the statements for that value # third it will be 'CS' then execute the statements for that value # then we're done with the loop :) # average = sum of numbers / number of numbers # average length is sum of the word lengths / number of words # What function will give us the size of a list? How do you find the size (length of a string)? # len() function :) # len( x ) function # Number of characters in a string x # Number of elements in a list x number_of_words = len( words ) # len( list ) is the number of elements in the list # ACCUMULATOR EXAMPLE 2: Number accumulation sum_word_lengths = 0 for w in words : # get current word's length len_w = len( w ) sum_word_lengths = len_w + sum_word_lengths # We modify the value of sum_word_lengths to say take the old value # and add another word's length to our sum so it adds up all the sums # echo current word and it's length print( w, ":", len_w ) print(sum_word_lengths) average = sum_word_lengths / number_of_words print( average ) # ACCUMULATORS # Number Accumulator Structure (different for addition/subtraction vs multiplication/division) # number_accumulator = 0 # for ... in ...: # number_accumulator = number_accumulator +/- modification (value we wanna add / subtract) # number_accumulator = 1 # for ... in ...: # number_accumulator = number_accumulator * or divide modification (value we wanna multiply/divide) # String Accumulator # string_accumulator = '' # for ... in ...: # string_accumulator = string_accumulator + string we wanna modify it with (add to the string accumulator through # concatenation) # List Accumulator # list_accumulator = [] # for ... in ...: # list_accumulator.append( what we wanna add to the list_accumulator )