Python for Kids Book: Project 7
September 4, 2015 3 Comments
In these posts I outline the contents of each project in my book Python For Kids For Dummies. If you have questions or comments about the project listed in the title post them here. Any improvements will also be listed here.
What’s in Project 7
Project 7 (Cryptopy) introduces dictionaries as a means of encoding text using a Caesar cipher. Along the way, you are introduced to the string module and the characters in string.printable. There is some discussion about escape sequences and examples of \n and \t are given. Since you don’t want to encrypt escape sequences the slicing operator is introduced in order to take string.printable and slice off the control characters. This becomes the character set that will be encoded.
An encryption dictionary is created and each of the characters in a test message are encrypted then joined using the join method of the empty string. I explain why this is better than adding one character after another to an existing string. I create a matching decryption function and decryption dictionary and test the round trip (plaintext-> ciphertext -> plaintext).
The project introduces file operations by reading a message from a file then writing the encrypted (or decrypted) message to another file. This is first done with the base file operations open and close, then the with keyword is introduced to make the housekeeping a little easier.
I demonstrate how to use your newly written encryption functions from the command line by importing the code from your own file – your own third party module! In order for this to work seamlessly you are introduced to the __name__ attribute and the if __name__ == “__main__”: construction.
On page 203, the first line of code:
is not necessary (because you already closed the file in the code on the previous page. It shouldn’t give you an error message though). Ignore it.
On page 209, there is a reference to code being in C:\Python27. This reference is only relevant if you are using Windows. On Linux and Mac things are more complicated – if the file is in the same directory that the shell is running from then you should be ok.