Python Chapters
Functions
The complete program for getting the message from a file ‘message.txt’, inputting the key, extending the key, enciphering and saving the ciphertext to a file ‘cipher.txt’ is given in the Appendix. You will perhaps feel that the program is getting a bit long to follow, and would be quite difficult to understand if you came back to it after a few weeks.
Python has a way of simplifying simplify a program by breaking it up into functional parts, or functions as they are called in programming. The main part of the program is simply the name of these functions like:
get message from file
remove punctuation and spaces
get the key from the keyboard
encipher
save the cipher to file
I think you’d agree that this is very easy to understand.
The functions themselves, each with its program code, are put before the main part of the program. This code is much easier to understand because the function name explains what the code is doing.
It is possible to send data (strings and variables) to a function and to get data back. Data is sent to a function by placing it at the end of the function name, enclosed in brackets. Data is received back from a function into the string or variable that preceded the function call. For example, to get the string called ‘message’ from a file named ‘plaintext.txt’ we could write this line in the main part of the program:
message = get_message(plaintext.txt)
Notice we must join together any words in the name of the function with a ‘_’.
Above the main part of the menu we write the program code for the function. We begin with a definition of its name and, in brackets, the data sent from main and finally a semi-colon. Note that the name of the data can be different in the function and in the main part of the program. We indent the rest of the function’s code by 4 spaces, showing it all belongs to the function. Finally we end with the ‘return’ command, followed with data to be returned to main in brackets:
def get_message(filename):
message=’’
fileid = open(filename,'r') #...’r’ is for “read”
message=fileid.read()
fileid.close()
return(message)
You can send to a function, or receive back, more than one piece of data. So to encipher we can write the command in main:
cipher = encipher(plain,key)
This will send both strings ‘plain’ and ‘key’ to the function. The code for the function, again placed before main, will be:
def encipher(plain,key):
n = 0
length=len(plain)
cipher=’’
for j in range(length):
position=alphabet.index(plain[j])
cipher = cipher + key[position]
n = n + 1
if (n==5):
cipher = cipher + ' '
n=0
return(cipher)
Note that the final line sends back the string ‘cipher’ to main.
Using these functions we can rewrite the program so that it’s easier to follow. The main part is simply:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
message= get_message('plaintext.txt')
plain=remove_spaces(message)
key=get_key()
key=extend_key(key)
cipher=encipher(plain,key)
save_ciphertext(‘cipher.txt’)
and the functions are written above main -- as shown in the Appendix.