Nov-04-2018, 09:44 PM
(Nov-04-2018, 02:36 AM)Drone4four Wrote: I understand what you are saying about how it might not be necessary to cast my string into a list however strings are immutable.Sure, that's one way to do it, by assigning a new value to the list's index, but because of this line...
(Nov-04-2018, 02:36 AM)Drone4four Wrote:...I had assumed you were building a new list that contained the shifted characters, without modifying the original list.scrambled_text =[] # Initializing output variable
Quote:You have the character you want to rotate. So the next step would be to find where that character is in thecharacter[0] = the corresponding character in shifted_alphabet
original
. Once you know the index of that, you use the same index to look up what the shifted character is in shifted
. Something likefor index, character in enumerate(text): lookup_index = original.find(character) new_character = shifted[lookup_index] # and then either... text[index] = new_character # ...or scrambled_text.append(new_character)So if you keep the
text = list(text)
line, you can assign a new value like so: text[index] = new_character
. Index the list itself, not a particular character within that list (which is a syntax/unsubscriptible error).Way earlier in the thread, I mentioned how we'd look at a "better" way to do this once you got it working. That better way is a translation table.
# original and shifted are from your code shifted = ''.join(shifted) # convert to a string # build a translation table, mapping one character to it's shifted version translation = str.maketrans(original, shifted) # now translate the text translated_text = str.translate("some text", original, shifted) print(translated_text)...though your teacher probably wouldn't want to see that, lol