Oct-28-2018, 12:34 AM
(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.