Why the local varible is not being printed

def before():
    global a
    a = 20
    print(a)
    def after():
        global a
        a = 30
        print(a)
    after()
    print(a)

before()

Why the last print statement not printing 20 as its in the before function() where local a = 20 so why it prints 30?


Solution 1:

def before():
  global a
  a = 20
  print(a)
def after():
  global a
  a = 30
  print(a)

after()
print(a)
before()

I think this is what your code looks like. And it looks like it does print 20, which makes sense since your letting a = 20. Your output should be: 30 30 20. Make sure you are formatting properly; indentation is huge in python because blocks of code do not utilize {} like many other languages.