Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caesar cipher
#6
(Oct-27-2018, 12:20 AM)stullis Wrote: The deque.rotate() method is altering the deque without providing an output. So, when you call alphabet.rotate(), you're changing alphabet.

I’m having an ‘ah-ha’ moment here. Like you’ve said, rotate() is not a function. It’s a method. It modifies the attribute of the object in its place. So it’s pointless to assign it as a variable. I’ve modified my script (below) by duplicating the original alphabet and the shifted alphabet. Then I proceed to manipulate the shifted alphabet in its place. Here is my code now:

from collections import deque
import string

def encrypt(text,shift):
    '''
    INPUT: text as a string and an integer for the shift value.
    OUTPUT: The shifted text after being run through the Caesar cipher.
    ''' 
    original_alphabet = string.ascii_lowercase # Initializing alphabet variable
    print(original_alphabet, 1) 
    original_alphabet = deque(list(original_alphabet)) # Turning alphabet into a list
    shifted_alphabet = original_alphabet 
    shifted_alphabet.rotate(shift) # Rotating new shifted alphabet
    original_alphabet = ''.join(original_alphabet) # Re-concatenating split list (alphabet)
    shifted_alphabet = ''.join(shifted_alphabet) # Re-concatenating split list (shifted alphabet)
    print(original_alphabet, 2)
    print(shifted_alphabet, 3)
    pass
encrypt(None,3)
Here is the output:

Quote:abcdefghijklmnopqrstuvwxyz 1
xyzabcdefghijklmnopqrstuvw 2
xyzabcdefghijklmnopqrstuvw 3

The second print statement at line 16 is still printing the shifted alphabet when I intended to print the original. @stullis I get that you’ve already addressed this point but I don’t understand what you mean when you say that since I have three printed items, they must all be printed in order. Yes, I see the three printed statements, but I am telling the Jupyter Notebook to print the original alphabet twice and the shifted alphabet once, rather than the reverse. I am still baffled.

Would someone be able to clarify further?

By the way, passing an integer in the three instances where the print function is invoked really helps.

Thanks stullis for your contribution so far.
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,162 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,761 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