Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caesar cipher
#5
I did some tests and found your problem. The first thing I noticed was your output (which matched mine):

Quote:abcdefghijklmnopqrstuvwxyz
xyzabcdefghijklmnopqrstuvw
None

Since your code calls print() three times and we get three printed items, they must be all three items printed in order. So, the second item is not alphabet_shifted; instead, it's alphabet being printed on line 14. To verify that, I did 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 Caesar cipher.
    '''
    alphabet = string.ascii_lowercase # Initializing alphabet variable
    print(alphabet, 1)
    alphabet = deque(list(alphabet)) # Turning alphabet into a list
    alphabet_shifted = alphabet.rotate(shift) # Assigning new variable to
    alphabet = ''.join(alphabet) # Re-concatenating split list
    print(alphabet, 2)
    print(alphabet_shifted)
    pass
    
encrypt("Hello again", 5)
Which produced:

Quote:abcdefghijklmnopqrstuvwxyz 1
vwxyzabcdefghijklmnopqrstu 2
None

The deque.rotate() method is altering the deque without providing an output. So, when you call alphabet.rotate(), you're changing alphabet. Because that method does not have a return, alphabet_shifted is instantiated as None which is why the third call to print prints "None".
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,847 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