''' Purpose: reinforce loop mechanics ''' # get some sequences color = input( 'Enter your favorite color: ' ) reply = input( 'Enter some names: ' ) names = reply.split() numbers = [ 12, 7, -11, -12, 59 ] print() print( 'color =', color ) print( 'names =', names ) print( 'numbers =', numbers ) print() input( "Enter when ready: " ) # one-by-one go through the characters in color for ch in color: #loop is going to look at the characters in the string color one-by-one # ch is assigned another character from color automatically by Python. #print( "ch:", ch ) up_ch = ch.upper() #using the loop variable to get the thing that I want print( up_ch ) # print the result print() input( "Enter when ready: " ) # one-by-one go through the names in names for name in names: # loop is go to process each name in names one at a time # each time in the loop name is set to another name in names #print( "name: ", name ) # print out the current loop variable cap_name = name.capitalize() # get a proper capitalization of name and put it in cap_name print( cap_name ) print() input( "Enter when ready: " ) # one-by-one go through the numbers in numbers for nbr in numbers: # loop through the numbers in numbers one at a time # nbr will be automatically set to the next number in numbers #print( "nbr: ", nbr ) comp_nbr = -nbr print( comp_nbr ) input( "Enter when ready: " ) # let's build a new string out of the color where the characters are separated # by blanks spaced_out_color = "" # when building a string your accumulator variable starts off as "" for ch in color: # need to consider the characters in color one at a time # ch is another character from color spaced_out_color = spaced_out_color + ch + " " # the new spaced out color is the old spaced out color plus the current character # plus a space. print( "spaced_out_color:", spaced_out_color ) print( spaced_out_color ) input( "Enter when ready: " ) # let's build a list of the number of characters in each one of the names name_lengths = [] # when building a list we are going to have an empty list as the # initial value for the accumulator for name in names : # consider each name in names # need to determine the length of the current name. print( "name:", name ) n = len( name ) # get the length of the current name print( "n:", n ) # add n to the list name_lengths name_lengths.append( n ) # adds n to the end of name_lengths print( "name_lengths:", name_lengths ) print( names ) print( name_lengths ) input( "Enter when ready: " ) # let's find the longest name length longest_name_length = max( name_lengths ) print( 'longest name length =', longest_name_length ) input( "Enter when ready: " ) i = name_lengths.index( longest_name_length) # want to determine the position of # longest name length in name_lengths. # that is the position is the position # of the longest name in names print( "i:", i ) longest_name = names[ i ] print( "longest_name:", longest_name ) # let's find the name that occurs first alphabetically first_alphabetically = min( names ) # find name that occurs first alphabetically print( 'first alphabetically: ', first_alphabetically )