Hacking Secret Ciphers with Python

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

For example, type in the following short program, save it as scope.py, and press F5 to run it:


Source code for scope.py



  1. spam = 42



  2. def eggs():

  3. spam = 99 # spam in this function is local

  4. print('In eggs():', spam)



  5. def ham():

  6. print('In ham():', spam) # spam in this function is global



  7. def bacon():

  8. global spam # spam in this function is global

  9. print('In bacon():', spam)

  10. spam = 0



  11. def CRASH():

  12. print(spam) # spam in this function is local

  13. spam = 0



  14. print(spam)

  15. eggs()

  16. print(spam)

  17. ham()

  18. print(spam)

  19. bacon()

  20. print(spam)

  21. CRASH()


The program will crash when Python executes line 1 6 , and the output will look like this:


42
In eggs(): 99
42
In ham(): 42
42
In bacon(): 42
0
Traceback (most recent call last):
File "C:\scope.py", line 27, in
CRASH()
File "C:\scope.py", line 16, in CRASH

Free download pdf