Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caesar cipher
#14
(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:
scrambled_text =[] # Initializing output variable
...I had assumed you were building a new list that contained the shifted characters, without modifying the original list.

Quote:
character[0] = the corresponding character in shifted_alphabet
You have the character you want to rotate. So the next step would be to find where that character is in the original. Once you know the index of that, you use the same index to look up what the shifted character is in shifted. Something like
for 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
Reply


Messages In This Thread
Caesar cipher - by Drone4four - Oct-26-2018, 03:06 PM
RE: Caesar cipher - by nilamo - Oct-26-2018, 03:12 PM
RE: Caesar cipher - by DeaD_EyE - Oct-26-2018, 07:48 PM
RE: Caesar cipher - by Drone4four - Oct-26-2018, 08:47 PM
RE: Caesar cipher - by stullis - Oct-27-2018, 12:20 AM
RE: Caesar cipher - by Drone4four - Oct-28-2018, 12:34 AM
RE: Caesar cipher - by knackwurstbagel - Oct-28-2018, 01:08 AM
RE: Caesar cipher - by stullis - Oct-28-2018, 01:47 AM
RE: Caesar cipher - by nilamo - Oct-28-2018, 03:43 AM
RE: Caesar cipher - by Drone4four - Nov-02-2018, 04:30 PM
RE: Caesar cipher - by nilamo - Nov-02-2018, 05:35 PM
RE: Caesar cipher - by Drone4four - Nov-04-2018, 02:36 AM
RE: Caesar cipher - by stullis - Nov-04-2018, 12:04 PM
RE: Caesar cipher - by nilamo - Nov-04-2018, 09:44 PM
RE: Caesar cipher - by Drone4four - Nov-06-2018, 02:13 AM
RE: Caesar cipher - by stullis - Nov-06-2018, 02:50 AM
RE: Caesar cipher - by Drone4four - Nov-09-2018, 07:22 PM
RE: Caesar cipher - by nilamo - Nov-09-2018, 07:50 PM
RE: Caesar cipher - by Drone4four - Nov-10-2018, 12:20 AM
RE: Caesar cipher - by nilamo - Nov-11-2018, 04:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cipher Caesar Azilkhan 1 2,163 Nov-21-2019, 03:40 PM
Last Post: ichabod801
  No idea how to use the Caesar Cypher in my code celtickodiak 5 3,120 Oct-08-2019, 03:29 AM
Last Post: stullis
  Monoalphabetic cipher pawlo392 1 12,843 Apr-01-2019, 08:51 PM
Last Post: ichabod801
  Vigenere and Caesar Cipher sammy2938 1 5,762 Jul-29-2017, 01:32 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020