''' Purpose: find the word of the longest length amongst the input ''' # get the words reply = input( "Enter text: " ) words = reply.split() # need to keep track of the longest word seen so far. It's initialization # should never prevent the word of longest length being missed longest_word_seen = '' # consider the words one by one for word in words : # 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 # first get the lengths of the two words we want to compare n_1 = len( longest_word_seen ) n_2 = len( word ) if ( n_2 > n_1 ) : # we no longer care about the old longest word, so we update it longest_word_seen = word else : pass # print( word, longest_word_seen ) # longest word so far must be longest as considered every word as a possibility # print longest word # print( longest_word_seen ) # all done # What if, instead of the longest word, we wanted to find all the words # that are the longest. (If there are ties, save them both) for word in words: # we have already found the longest word seen n_1 = len( longest_word_seen ) n_2 = len( word ) # print any words with the same length as the longest word # == for comparison! # = for assignment of variables! if ( n_1 == n_2 ): print( word )