''' Purpose: get an input code phrase and list of indices. Determine the hidden message by using the indices to peek into the code phrase ''' # get the code phrase that contains hidden message reply = input( "Enter code phrase: " ) print() # clean up reply to get code phrase with no leading or trailing whitespace code_phrase = reply.strip() # doesn't change reply just stores stripped version of reply in code_phrase # 'Clean up' # strip() function - get rid of leading and trailing whitespace # print the code phrase print( "Code phrase=", code_phrase ) print() # get the list of indices (as string) for peeking into code phrase reply = input( "Enter indices for looking into code phrase: " ) # ex. '1 2 3' print() # convert reply into a list of numeric strings numeric_strings = reply.split() # ex. ['1','2','3'] print( numeric_strings ) # build one-by-one the list of indices out of the numeric strings # Convert list of numeric strings to list of integers (like slist to nlist in summing.py) # You cannot CANNOT CANNOT call int(numeric_strings) - you can't call the int function on the list # of numeric strings! indices = [] # Initialize accumulator to empty list (we wanna build up a list of strings) for ns in numeric_strings: # ns is the loop variable (for numeric string) in the numeric_strings list # convert each numeric string one by one ex. '1' becomes 1 stored in i i = int( ns ) # add the integer version of ns to indices (list of integers) indices.append( i ) # print the list of indices print( "Indices:", indices ) # ex. indices is [1,2,3] if numeric_strings was ['1','2','3'] print() # STRING ACCUMULATOR EXAMPLE # build secret message (string) by peeking into code phrase using the # indices one-by-one message = '' # accumulator # Use the indices list and the code_phrase string and get the characters in code_phrase at those indices # to create a secret message # Each index in the indices list is adding a character to the message! # What do we want to loop through? How are we going to determine the characters at the indices in our indices list? # indices..... list................. characters...... subscripting.... hm.... # How can we use indices (list of numbers) and code_phrase (string we take characters from)? # indices is the list of numbers while numeric_strings is the lsit of numeric strings # WHY WE USE INDICES INSTEAD OF NUMERIC_STRINGS # You need the NUMBER (int) versions of numbers to subscript (to call code_phrase[i]) # You don't want to do code_phrase['2'] you want to use code_phrase[2] # You can't use the numeric string as an index like code_phrase['2'] which is why # we use the list of integers indices we created from numeric_strings for i in indices: # Get each index (number) from the list of indices (ex. [1,2,3]) print( 'Loop variable', i ) ch = code_phrase[i] # Get character at code_phrase[i] where i will be a number index - subscripting! print( 'Character at index i:', ch ) message = message + ch # string_accumulator = string_accumulator + modification (decoded character to add) print( 'Message so far:', message ) # print secret message print( "Secret message:", message ) # computer # 01234567 # indices is [3,6,5] # p is at index 3 # e is at index 6 # t is at index 5 # 'pet' is the final message (string we built up)