''' Purpose: get an input code phrase and list of indices. Determine the hidden message by using the indices to peek into the code phrase ''' # remember () for functions!!! # 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() # strip() removes leading and trailing whitespaces # cases don't matter in this case # 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: " ) print() # convert reply into a list of numeric strings numeric_strings = reply.split() # build one-by-one the list of indices out of the numeric strings # build a list of integers from the list of numeric strings indices = [] # list accumulator, starts with empty list # for each numeric string in numeric_strings, convert to an int and put it into the list indices # (use i for ints for range stuff, generally - just a convention) for s in numeric_strings: index = int( s ) # s = numeric string --> int(s) --> convert to int version indices.append( index ) # remember, you don't have to assign indices = indices.append( index ) # append() will update the original list --> puts index at the end of list indices # we must convert our numeric string into ints because indices will ONLY TAKE INTS! # no strings nor floats as our positions/indices # print the list of indices print( "Indices:", indices ) print() # build secret message (string) by peeking into code phrase using the # indices one-by-one message = '' # string accumulator starts with empty string ('') as we build from scratch # '' indicates that our accumulator is a string, so we use + to build up a new string # we already know we want to produce a string, so to start our accumulator, we start # with '' # if we know that we want to build a list, then the accumulator starts with [] # if we know that we want a running sum, then the accumulator starts with 0 # message = [] # we need to peak in the original message at the indices we said to look at # for each index in the list indices, we look into the original message (code_phrase) at that index for i in indices: ch = code_phrase[ i ] # string indices ALWAYS USE BRACKETS [] NOT () # print( i, ch ) message = message + ch # each time through, take the current message and then glue on the next # character at the end of the current message using + --> update it # message.append(ch) # if message is a list, but the problem says to give back a string # don't append() for strings!!! only concatenate for strings # append() is ONLY for lists # CANNOT + with a list # string subscripting allows use of one int or a range of things using : inside [] # s = computer # 01234567 # s[3] = 'p' # s[0] = 'c' # s[5] = 't' # s[9] = not valid--> error (out of range) # print secret message print( "Secret message:", message )