''' Purpose: reinforce loop mechanics ''' # get some sequences color = input( 'Enter your favorite color: ' ) reply = input( 'Enter some names: ' ) names = reply.split() # split it into a list numbers = [ 12, 7, -11, -12, 59 ] # this is a literal list, [val1, val2, val3] , all values defined explicitly print() print( 'color =', color ) # color is a string print( 'names =', names ) # names is a list print( 'numbers =', numbers ) # numbers is a list print() # one-by-one go through the characters in color for ch in color: # ch is our loop variable, we don't have to assign it anywhere, python just knows # if it helps you to change ch to current_character, do that. understanding loop variables is important up_ch = ch.upper() # capitalize every letter - this is a new string stored in up_ch, ch is unchanged print( up_ch ) # this print() is in the for loop, so it will run every time the loop runs print() # one-by-one go through the names in names for name in names: # if you separate names with commas, the commas will be included in the string name cap_name = name.capitalize() # capitalize first letter and store it in cap_name, name is unchanged print( cap_name ) print() # one-by-one go through the numbers in numbers for nbr in numbers: comp_nbr = -nbr # get the complement ( number * -1 ) print( comp_nbr ) input( "enter when ready " ) # let's build a new string out of the color where the characters are separated # by blanks # this is a reeeaaaaaally important topic, pay attention and ask questions as needed # accumulation = building things up. we define a variable outside of a loop and change it on every run of the loop # accumulators are defined differently based on what you want to build up # ex: string accumulator - we want to build a string, so we have to initialize spaced_out_color as a string # when we want to build a string within a loop, we initialize it it as an empty string "" or '' spaced_out_color = "" # when we are building a string, we start with an empty string as our accumulator for ch in color: spaced_out_color = spaced_out_color + ch + " " # update with the old version + something new # new spaced_out is the old spaced_out + new character + a space # when we do each run, the last character in spaced_out_color will be a space print( spaced_out_color ) # this print() is outside of the for loop, so it will only run once # our accumulator is always going to depend on the problem and what we're trying to get input( "enter when ready " ) print() # let's build a list of the number of characters in each one of the names # when we're building a list, we start with an empty list name_lengths = [] # this is our accumulator for name in names : # now we add to it in our loop n = len( name ) # this is our name length, calculated using the len() function # len() hands back an integer of character count, not a numeric string # print("name:",name) never be afraid to add print() statements to check your work! # print("length:",n) name_lengths.append( n ) # add the name length to the list name_lengths using the append() method function # remember how we talk about strings being immutable? that's why we have to say s_l = s.lower() # lists are mutable, so we can modify them directly without assignment statements # GOTCHA: NEVER ASSIGN AN APPEND() # print(name_lengths) # this print() is in the for loop, so it will run every time #print() print( names ) print( name_lengths ) input( "enter when ready " ) # these are so we can look at the program slowly # let's find the longest name length longest_name_length = max( name_lengths ) # do we have a function that would help with this? do we have a list of lengths? # we already found all the lengths, so we can just find the max() number # the spot of the maximum length is the same spot of the longest name in names print( 'longest name length =', longest_name_length ) # let's find the index of longest_name_length i = name_lengths.index( longest_name_length ) # i will be our index of where we found # index() is for lists, find() is for strings... it's hard for all of us print( "index:", i ) # let's find the longest name that comes first in the list # to find all of the longest names, we'd need if statements, which we'll get to after test 1 probably longest_name = names[ i ] # go into names, grab what's at position i print("longest name:",longest_name ) # let's find the name that occurs first alphabetically first_alphabetically = min( names ) # min() and max() operate on sequences: strings, lists, tuples print( 'first alphabetically: ', first_alphabetically )