Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caesar cipher
#16
You're getting the extra "w"s because of an interaction between the output of str.find() and str[index]. The "original" string does not include a space as a character, therefore, str.find() returns a -1 per the documentation. If you index an iterable with a -1, the interpreter returns the last item in the iterable:

x = "abc"
print(x[-1]) # Prints "c"
So, every time your loop encounters a space...

  1. it searches original for the character
  2. fails to find it and returns a -1
  3. indexes shifted with a -1 to return the last character ("w" in this case)
  4. appends that character in place of the space

To correct this, you'll either have to control for a -1 result from line 19 or rewrite the loop to avoid the issue. You could split "text" by spaces and then join it again later after encrypting.
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,164 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,846 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