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
- spam = 42
- def eggs():
- spam = 99 # spam in this function is local
- print('In eggs():', spam)
- def ham():
- print('In ham():', spam) # spam in this function is global
- def bacon():
- global spam # spam in this function is global
- print('In bacon():', spam)
- spam = 0
- def CRASH():
- print(spam) # spam in this function is local
- spam = 0
- print(spam)
- eggs()
- print(spam)
- ham()
- print(spam)
- bacon()
- print(spam)
- 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