Hacking Secret Ciphers with Python

(Ann) #1
Chapter 6 – The Caesar Cipher 85

Displaying and Copying the Encrypted/Decrypted String


caesarCipher.py



  1. print the encrypted/decrypted string to the screen



  2. print(translated)




  3. copy the encrypted/decrypted string to the clipboard



  4. pyperclip.copy(translated)


Line 49 has no indentation, which means it is the first line after the block that started on line 26
(the for loop’s block). By the time the program execution reaches line 49, it has looped through
each character in the message string, encrypted (or decrypted) the characters, and added them to
translated.


Line 49 will call the print() function to display the translated string on the screen. Notice
that this is the only print() call in the entire program. The computer does a lot of work
encrypting every letter in message, handling wrap-around, and handling non-letter characters.
But the user doesn’t need to see this. The user just needs to see the final string in translated.


Line 52 calls a function that is inside the pyperclip module. The function’s name is copy()
and it takes one string argument. Because copy() is a function in the pyperclip module, we
have to tell Python this by putting pyperclip. in front of the function name. If we type
copy(translated) instead of pyperclip.copy(translated), Python will give us an
error message.


You can see this error message for yourself by typing this code in the interactive shell:





copy('Hello')
Traceback (most recent call last):
File "", line 1, in
NameError: name 'copy' is not defined





Also, if you forget the import pyperclip line before trying to call pyperclip.copy(),
Python will give an error message. Try typing this into the interactive shell:





pyperclip.copy('Hello')
Traceback (most recent call last):
File "", line 1, in
NameError: name 'pyperclip' is not defined




Free download pdf