codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

local variable 'p' referenced before assignment


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