''' Purpose: find the word of the longest length amongst the input ''' # get the words reply = input( "Enter a series of words: " ) words = reply.split() # List of words # need to keep track of the longest word seen so far. It's initialization # should never prevent a word of longest length being missed # Should we initialize the longest_word variable outside or inside? # It should be outside (similar to an accumulator) because if you # initialize it inside the for loop it gets reset to the initial value of the # longest_word in the beginning for EVERY time you go through the loop # and you don't want that. (If you have the longest word as '' inside the # for loop, you reset the longest_word as '' each time you compare a new # word.) # consider the words one by one longest_word_seen = "" for word in words : # We are iterating through our words so our current word # is the word we're on in the loop! We're going one by one and comparing # the words to the longest_word_seen! # check the word out - compare its length to longest so far # if current word is longer than the longest word so far, need to update # >= if you want the last longest word if ( len(word) > len(longest_word_seen) ): longest_word_seen = word # longest_word_seen will become word when? # When the length of the current word I'm on is greater than # the length of the longest_word_seen! # longest word so far must be longest as considered every word as a possibility # print longest word print( longest_word_seen )