Unable to bind the local x of f() to the global var x of the nested g(). Why?
def f():
 x=0
 def g():
  global x
  x+=1
  print(x)
 g()
 g() # added to make seemingly more practical 
-
>>> f()
...
NameError: global name 'x' is not defined
Unable to bind the local x of f() to the global var x of the nested g(). Why?
def f():
 x=0
 def g():
  global x
  x+=1
  print(x)
 g()
 g() # added to make seemingly more practical 
-
>>> f()
...
NameError: global name 'x' is not defined
 
    
    You want to make x a global variable in function f():
def f():
    global x
    x = 0
    def g():
        global x
        x += 1
        print(x)
    g()
f()
# 1
x is pointless here, so it's better you pass x as argument to g() like so:
def f():
    x = 0
    def g(x):
        x += 1
        print(x)
    g(x)
f()
# 1
This not only makes your code more concise but also removes the overhead of 'global's.
 
    
    To avoid using globals, simply pass a parameter through the function g:
def f():
    x=0
    def g(y):
        y+=1
        print(y)
    g(x)
f()
This should work.
 
    
    