''' Purpose: find the word of the longest length amongst the input ''' # get the words reply = input( 'Enter a series of words: ' ) 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_so_far = '' # Or we can do the first word in the list, but if there are no words in the list # the empty string is correct # 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 # print( 'word =', word, ': longest word =', longest_word_seen_so_far) len1 = len( word ) len2 = len( longest_word_seen_so_far ) if ( len1 > len2 ) : longest_word_seen_so_far = word # longest word so far must be longest as considered every word as a possibility # print longest word print( longest_word_seen_so_far ) # all done # Alternative solution list_of_lens = [] for word in words: lenw = len( word ) list_of_lens.append( lenw ) # list_of_lens is the individual word lengths # print( 'list of lens', list_of_lens ) # the max length word max_word_len = max( list_of_lens ) # print( 'max word len', max_word_len ) n = len( words ) # The ith word in words has the ith length in list_of_lens # for i in range( 0, n): # print( words[i] , list_of_lens[i] ) index_of_longest_word_len = list_of_lens.index( max_word_len ) # print( 'index of longest word len', index_of_longest_word_len ) longest_word = words[ index_of_longest_word_len ] print( longest_word )