Hacking Secret Ciphers with Python

(Ann) #1
Chapter 8 – The Transposition Cipher, Encrypting 101


  1. print('Done.')


Save this program with the name helloFunction.py and run it by pressing F5. The output looks
like this:


Start!
Hello!
42 plus 1 is 43
Call it again.
Hello!
42 plus 1 is 43
Done.


When the helloFunction.py program runs, the execution starts at the top. The first line is a def
statement that defines the hello() function. The execution skips the block after the def
statement and executes the print('Start!') line. This is why 'Start!' is the first string
printed when we run the program.


The next line after print('Start!') is a function call to our hello() function. The
program execution jumps to the first line in the hello() function’s block on line 2. This
function will cause the strings 'Hello!' and '42 plus 1 is 43' to be printed to the
screen.


When the program execution reaches the bottom of the def statement, the execution will jump
back to the line after the line that originally called the function (line 7). In helloFunction.py, this
is the print('Call it again.') line. Line 8 is another call to the hello() function. The
program execution will jump back into the hello() function and execute the code there again.
This is why 'Hello!' and '42 plus 1 is 43' are displayed on the screen two times.


After that function returns to line 9, the print('Done.') line executes. This is the last line in
our program, so the program exits.


The Program’s main() Function


transpositionEncrypt.py


  1. def main():

  2. myMessage = 'Common sense is not so common.'

  3. myKey = 8

Free download pdf