local variable 'p' referenced before assignment

Pankaj Jangid pankaj.jangid at gmail.com
Sun Sep 8 05:54:27 EDT 2019


Why this code is giving local variable access error when I am accessing
a global variable?

p = 0
def visit():
    m = 1
    if m > p:
        p = m

visit()
print(p)

If I change the variable assignment inside the function to q = m then it
works fine. Like this

p = 0
def visit():
    m = 1
    if m > p:
        q = m    # Changed 'p' to 'q'

visit()
print(p)

-- 
Pankaj Jangid



More information about the Python-list mailing list