Oct-26-2018, 03:06 PM
I’ve begun writing a basic Caesar cipher as part of a Udemy course by Jose Portilla that I am taking for fun.
I am writing the encryption function. Jose has included some helpful pseudo code and a doc string to sort of get his students started.
Here is the docstring and some pseudo code provided by the instructor:
The issue I’ve encountered is when I attempt to add the for loop between lines 14 and 15 so my script now looks like this:
The problem clearly is with my enumeration loop. What is actually happening in the loop as it appears now is this: for every item in the string (the string passed into the function - - there are 70 or so characters in total), it’s printing the full text. That’s not at all what I want. I want to apply the rotated alphabet for each character in the string. I am really struggling. Can anyone provide a little advice without completing it all for me?
I’ve had some previous help with enumeration on this forum in my thread titled “Printing lines in a basic text file”. Stack Overflow’s “What does enumerate mean?” is helpful too.
The course material I am working with can be found on Jose Portilla’s GitHub page. Here is the repo’s top level directory. The module I am working on is #6 called “Hacking incident”.
I am writing the encryption function. Jose has included some helpful pseudo code and a doc string to sort of get his students started.
Here is the docstring and some pseudo code provided by the instructor:
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. passHere is my work in progress:
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 Caeser cipher. ''' alphab = string.ascii_lowercase print(alphab) alphab = deque(list(alphab)) print(alphab) alphab.rotate(shift) print(alphab) alphab = ''.join(alphab) print(alphab) passWhen I call the function with
encrypt(None,3)
, it produces this as output:Quote:abcdefghijklmnopqrstuvwxyzThat demonstrates that I have some of the basic functionality in place and working.
deque(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])
deque(['x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'])
xyzabcdefghijklmnopqrstuvw
The issue I’ve encountered is when I attempt to add the for loop between lines 14 and 15 so my script now looks like this:
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 Caeser cipher. ''' alphab = string.ascii_lowercase print(alphab) alphab = deque(list(alphab)) print(alphab) alphab.rotate(shift) print(alphab) for index, alphab in enumerate(text): # ?????????? I"m not sure how to proceed, ugh print(index, text) alphab = ''.join(alphab) print(alphab) passWhen I call the function with
encrypt('Helter Skelter! Feed the birds and deploy the vicious little piggies!',3)
, that produces this:Quote:abcdefghijklmnopqrstuvwxyz
deque(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])
deque(['x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'])
0 Helter Skelter! Feed the birds and deploy the vicious little piggies!
1 Helter Skelter! Feed the birds and deploy the vicious little piggies!
2 Helter Skelter! Feed the birds and deploy the vicious little piggies!
3 Helter Skelter! Feed the birds and deploy the vicious little piggies!
4 Helter Skelter! Feed the birds and deploy the vicious little piggies!
5 Helter Skelter! Feed the birds and deploy the vicious little piggies!
. . .
65 Helter Skelter! Feed the birds and deploy the vicious little piggies!
66 Helter Skelter! Feed the birds and deploy the vicious little piggies!
67 Helter Skelter! Feed the birds and deploy the vicious little piggies!
68 Helter Skelter! Feed the birds and deploy the vicious little piggies!
!
The problem clearly is with my enumeration loop. What is actually happening in the loop as it appears now is this: for every item in the string (the string passed into the function - - there are 70 or so characters in total), it’s printing the full text. That’s not at all what I want. I want to apply the rotated alphabet for each character in the string. I am really struggling. Can anyone provide a little advice without completing it all for me?
I’ve had some previous help with enumeration on this forum in my thread titled “Printing lines in a basic text file”. Stack Overflow’s “What does enumerate mean?” is helpful too.
The course material I am working with can be found on Jose Portilla’s GitHub page. Here is the repo’s top level directory. The module I am working on is #6 called “Hacking incident”.