def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"
    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)

""" Prints:
After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam
"""
print()


def f():
  a = "a"
  for a in range(3):    
    print(a)        
f()

""" Prints:
0
1
2
"""
print()

x = 'x'
y = 'y'
def f():
  print(y)
  try:
    print(x)
  except Exception as e:
    print("we can't refer to the x declared outside this function because "\
          "we are reusing it in this function. "\
          "there is no good reason to reuse variable names like this!"
          )
  x = 2

f()
print(x)

""" Prints:
y
we can't refer to the x declared outside this function because we are reusing it in this function. there is no good reason to reuse variable names like this!
x
"""