Python for Kids Book: Project 7

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:

>>> file_object.close()

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.

6 Responses to Python for Kids Book: Project 7

  1. Pingback: Python for Kids: Python 3 – Project 7 | Python Tutorials for Kids 13+

  2. Ax says:

    Not working with decipher

    • brendanscott says:

      When something is not working, you should:
      * check that you have typed in everything correctly – cross check with the code downloads if you still have trouble (see code samples link in right sidebar)
      * have a look at any messages that Python gives you. These often tell you or give you a clue as to what has gone wrong.

  3. Jane From Hong Kong says:

    Question on ENCRYPT constant: In the book ENCRYPT is set to False, input file is opened, and contents read. It is unclear how the decrypt_msg function could ever be called. When and how does ENCRYPT become True so decrypt_msg function is called?
    The code doesn’t have a mechanism to determine whether file contents are encrypted or plaintext, set ENCRYPT to either true of false, then apply the corresponding encrypt_msg or decrypt_msg function.

    • brendanscott says:

      This is covered from pages 212 ff. Basically you change the value of ENCRYPT yourself. When you want to encrypt, set it to True. If you want to decrypt set it to False. . You can write some code to set it from within the program (look at project 3 for inspiration). I didn’t do it in the book because I felt it would make the code a little over complicated.

      Alternatively, you could make two programs, one for encryption and one for decryption (where the only difference is the value of ENCRYPT).

      • Jane From Hong Kong says:

        Thank you for the prompt explanation and thank you very much for writing this wonderful book!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: