Entering the message to encipher

The enciphering program takes a message and, with a key, produces a cipher that will look meaningless, effectively hiding the message. Let’s say our message is:

I must go down to the seas again, for the call of the
running tide is a wild call, and a clear call, that
may not be denied.

We have to get this message into the computer. In Idle, we click on File|New Window

 

to get a blank page on which to write the program. Now we type into our program a name, an equals sign and the message enclosed by triple inverted commas. I've chosen the name 'message' but you can choose almost any name you like, such as 'plaintext' or 'pt' or 'p49'.

message = ‘’’I must go down to the seas again, for the call of the
running tide is a wild call, and a clear call, that
may not be denied.’’’

The message is called a string of letters. Each character is designated by an order number, beginning with zero. So message[0] is ‘I’, message [1] is a space, message[2] is ‘m’ and so on. This ability to designate a letter by its position in a string is an essential part of programming.

Instead of typing the message letter by letter, you can copy and paste it. Whichever way you do it, the message is long enough to cover several lines. We use triple inverted commas around the message to tell python that it’s all one string.

We can measure the length of this string using the python command len() in the following way:

length= len(message)

This line counts the characters in the message, including the spaces, and puts the number in the entity I have called ‘length’. You could have chosen any other name for this entity that appeals to you, like ‘x’ or leng or len1. But you can’t call it ‘len’ because that is reserved as a command.

The name ‘length’ stands for a number and is called a ‘variable’. On my computer, length is 134. It may be different on yours if you’ve typed the message into fewer lines than me, and so have fewer spaces.

We can get the computer to display the message in Python Shell, character by character, with these commands:

for j in range(length):
    print (message[j],end="")

You will notice that the first line ends with a colon ‘:’ and the second line is indented four spaces.

The words of the first line mean: make the variable ‘j’ equal to zero, then make it 1, then 2 … all the way up to, but not including, the value of ‘length’ (which you’ll recall is the length of the cipher). The colon and the indentation mean: for every value in the first line carry out the instruction in the second line.

In other words the instruction is print message[0], then print message[1], then message[2]... and so on up to message[length-1]; after that ‘j’ gets to the value of ‘length’ and the routine is finished.

You can indent a whole series of actions to be carried out for every value of ‘j’. So we could write:

for j in range(length):
    print (‘The value of j is = ’,end="")
    print (j,end="")
    print (‘ and the letter is ’,end="")
    print (message[j])
print (‘All done’)

The computer would then print out:

The value of j = 0 and the letter is I
The value of j = 1 and the letter is (this is the space) 
The value of j = 2 and the letter is m
The value of j = 3 and the letter is u
The value of j = 4 and the letter is s
The value of j = 5 and the letter is t
…all the way up to…
The value of j = 133 and the letter is .

When ‘j’ gets to the value of length the routine is completed. The computer then goes on to the next command, which you can see is not indented, and prints out “All done”.

A final comment – the comma at the end of a print command means ‘don’t end the line’. When the comma is omitted, python will print and then start a new line.

Go ahead and write this program and run it and see what happens. 

The next step is to ensure all letters in the message are in lower case and that any punctuation is removed. Converting to lower case is easily done with the special python command:

message = message.lower()

which takes all the steps necessary to convert any upper case letter in the string 'message' to its equivalent lower case letter.

To get rid of punctuation is a bit more complicated. We shall instruct the computer to take each character in the string 'message', then test to see whether it is a letter between 'a' and 'z', and if so to add it to a new string which I’ll call 'plain'. To do this I first have to tell python that 'plain' is a string, otherwise python could equally well think ‘plain’ is a number variable. A string is enclosed by an inverted comma at each end. In this case the string is empty so we write:

plain = ‘’

Here’s the program code to extract the letters from ‘message’ and put them into ‘plain’. Pay particular attention to the indenting which must regularly be 4 spaces, and don’t forget the semi colons! The lines in red beginning with # are comments to help understand the program; the hash tells python they are not part of the program.

#..declare ‘plain’ as an empty string..
plain=’’ 
#..take each letter of message & if it's between a and z add it to
# the new string 'plain'.
for j in range(length):
    if (message[j] >='a' and message[j]<='z'):
        plain = plain + message[j]
#...display the list called ‘plain’ in the Shell & its length..
print ("plain= ",end="")
for j in range(length):
    print (plain[j],end="")
print ("length=",length)

Add these lines to your program.  The complete program is given in Appendix 1 and you can copy it from there if you like. Save the program with File|Save and run it by pressing the F5 key or with File|Run.

Now the punctuation and spaces between words have gone, the letters are all lower case and the length has shrunk appropriately to 93. Here is the output you will get:

plain=imustgodowntotheseasagainforthecalloftherunningtideisawildcallandaclearcallthatmaynotbedenied
length= 93

PREVIOUS          NEXT