Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caesar cipher
#10
Hooray! Now my original alphabet and shifted alphabet print as intended. Here is my new script:

from collections import deque
import string
import copy

def encrypt(text,shift_variance):
    '''
    INPUT: text as a string and an integer for the shift value.
    OUTPUT: The shifted text after being run through the Caesar cipher.
    ''' 
    original = string.ascii_lowercase # Initializing alphabet variable
    print(original, 1) 
    original = deque(list(original)) # Turning the original alphabet into a list
    shifted = original.copy() # Assigning new variable to copy of original alphabet
    shifted.rotate(shift_variance) # Rotating new shifted alphabet 
    original = ''.join(original) # Re-concatenating split list (alphabet)
    shifted = ''.join(shifted) # Re-concatenating split list (shifted alphabet)
    print(original, 2)
    print(shifted, 3)
    pass
At line thirteen I invoke the copy function from the copy module. I learned about this technique by Googling around for ‘python copy reference’ and similar search terms as introduced by @knackwurstbagel and @nilamo. In particular, the explanation found over in a Stackoverflow question titled “How to clone or copy a list?“ helped tremendously.

Here is my new output:

Quote:abcdefghijklmnopqrstuvwxyz 1
abcdefghijklmnopqrstuvwxyz 2
xyzabcdefghijklmnopqrstuvw 3

Now we can return to writing a for loop to loop through each character in the message entered by the user.

As a reminder, here is the original doc string and pseudo code provided by the instructor for the encrypt function:

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 Caeser cipher.
    '''
    
    # Create a normal plain alphabet
    
    # Create a shifted version of this alphabet 
    # (Try slicing using the shift and then reconcatenating the two parts)
    
    # Use a for loop to go through each character in the original message.
    # Then figure out its index match in the shifted alphabet and replace.
    # It might be helpful to create an output variable to hold the new message.
    
    # Keep in mind you may want to skip punctuation with an if statement.
    
    # Return the shifted message. Use ''.join() method 
    # if you still have it as a list.
            
    pass
I can check off a number of these lines as already implemented. I’ve created a plain alphabet and created a shifted alphabet. I’ve returned a shifted alphabet and joined them together. Now I am going to tackle the for loop.

Here is my script:

from collections import deque
import string
import copy

def encrypt(text,shift_variance):
    '''
    INPUT: text as a string and an integer for the shift value.
    OUTPUT: The shifted text after being run through the Caesar cipher.
    ''' 
    original = string.ascii_lowercase # Initializing alphabet variable
    original = deque(list(original)) # Turning the original alphabet into a list
    shifted = original.copy() # Assigning new variable to copy of original alphabet
    shifted.rotate(shift_variance) # Rotating new shifted alphabet 

    # BEGIN for loop:
    text = list(text) # Convert text to string
    print(text) # Confirmation of conversion operation
    scrambled_text =[] # Initializing output variable 
    for index in text:
        for variable, char in enumerate(shifted):
            variable[char] = scrambled_text        
    pass
When I call the encryption function using encrypt("Hello World",3), I get this output:

Quote:['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']


---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-51-83dd904990e0> in <module>()
----> 1 encrypt("Hello World",3)

<ipython-input-50-a466565b77aa> in encrypt(text, shift_variance)
19 for index in text:
20 for variable, char in enumerate(shifted):
---> 21 variable[char] = scrambled_text
22 pass

TypeError: 'int' object does not support item assignment

What is going wrong in my script?

shifted is the list of characters in the shifted alphabet, so it’s iterable. I Googled ‘TypeError: 'int' object does not support item assignment’ which turned up a Stackoverflow question with that TypeError in the title but the explanation completely escapes me.

I'm trying to use a for loop to go through each character in the original message and then assign a new letter from the shifted alphabet to each index match in the input text. Or in other words, what I am stuck on the most here is how to come up with the algorithm (a for loop or combination of two for loops) to 'replace' one character in the input text with a letter in the shifted alphabet. When "replacing" is used by the instructor in the pseudo code, I am not sure how to translate that into Python code. This is my biggest hurdle. Can someone here provide some guidance and clarity?

Other resources I’ve used include Ned Batchelder’s talk on YouTube titled “Loop like a native: while, for, iterators, generators”.

I’ve had some previous help with enumeration on this forum in my thread titled “Printing lines in a basic text file” along with Stackoverflow’s “What does enumerate mean?”.
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