''' Purpose: find the word of the longest length amongst the input ''' # get the words reply = input( 'Enter text to be examined:: ' ) words = reply.split() #words is a list # 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 = '' #we havent seen anything yet # consider the words one by one for word in words : #for each word in words, see if it is longest than the longest word we have seen so far #then we want to update longest_word_seen_so_far #we are working on problem solving! #we are about the current word, and the longest word so far, and their respective lengths #now we want to compare these two, and then update if we have seen something better # 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 nw = len( word ) nl = len ( longest_word_seen_so_far ) #len only works on strings and sequences, will not work on numbers #now check the two, update if when nw > nl if ( (nw > nl ) == True ) : #its true when both sides match (nw > nl) , and false when they arent equal #nw > nl is either true or false, so you don't need to check again if it is true or not #the if statement only continues if the statement is true, inherently nw > nl is either true or flase #but if it helps you learn/visualize , go ahead #we know that the new word is the longest word seen so far longest_word_seen_so_far = word #what happens if they have the same length? #go for the first one # the comparision is only greater than NOT greater than or equal! #if it was greater than or equal, it would update the longest word with the last value that have equal lengths else : pass # pass says - i know what I am doing there is no work needed right here #i thought about it but there is no action needed with this else statement print( "processing: " , word , longest_word_seen_so_far ) # 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