Variable referenced before assignment error in prime factorization program?
<p>I am writing some code that is supposed to find the prime factorization of numbers. The main function increments through numbers; I'm doing that because I want to use the code to conduct timing experiments. I don't mind it not being super-efficient, part of the project for me will be making it more efficient myself. It is also not yet totally complete.</p>

<pre><code>import math
import time

primfac=[]

def primcheck(n):
    for x in xrange(2, int(n**0.5)+1):
        if n % x == 0:
            return False
    return True

def primes(n):
    sieve = [True] * n
    for i in xrange(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
    return [2] + [i for i in xrange(3,n,2) if sieve[i]]


def factfind(lsp,n): #finds factors of n among primes
    for i in lsp:
        if n%i==0:
            primfac.append(i)
        else:
            i+=1

def simplify(lsp, n):
    x = 1
    for i in lsp:
        x=i*x
    if x != n:
        print "needs exponent, computing"
        for i in lsp:
            y=n/i
            if primcheck(y) == True:
                lsp.append(y)
            else:
                lsp.append(factfind(primes,y))

def primfacfind(n1,n2):
    while n1 &lt;= n2:
        time_start = time.clock()
        if primcheck(n1) == True:
            print "prime"
            time_elapsed = time.clock() - time_start
            print "time:", time_elapsed
            n1+=1
        else:
            n = n1
            print "starting #", n

            factfind(primes(n),n)
            print primfac

            del primfac
            primfac[:] = []
            simplify(primfac, n)

            time_elapsed = time.clock() - time_start
            print "time:", time_elapsed

            n1+=1

primfacfind(6,15)
</code></pre>

<p>When I run the code, it gives this error message:</p>

<pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
  File "python", line 65, in &lt;module&gt;
  File "python", line 54, in primfacfind
UnboundLocalError: local variable 'primfac' referenced before assignment
</code></pre>

<p>Which I don't understand because I've tested every function except the <code>simplify</code> function and even then the only new part is the lines after <code>print</code>.</p>

<p>Functions in python can only access variables declared outside of function scope read-only without the global keyword.</p>

<pre><code>import math 
import time

primfac=[]

def primcheck(n):
    for x in xrange(2, int(n**0.5)+1):
        if n % x == 0:
            return False
    return True

def primes(n):
    sieve = [True] * n
    for i in xrange(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
    return [2] + [i for i in xrange(3,n,2) if sieve[i]]


def factfind(lsp,n): #finds factors of n among primes
    for i in lsp:
        if n%i==0:
            primfac.append(i)
        else:
            i+=1

def simplify(lsp, n):
    x = 1
    for i in lsp:
        x=i*x
    if x != n:
        print "needs exponent, computing"
        for i in lsp:
            y=n/i
            if primcheck(y) == True:
                lsp.append(y)
            else:
                lsp.append(factfind(primes,y))

def primfacfind(n1,n2):
    global primfac
    while n1 &lt;= n2:
        time_start = time.clock()
        if primcheck(n1) == True:
            print "prime"
            time_elapsed = time.clock() - time_start
            print "time:", time_elapsed
            n1+=1
        else:
            n = n1
            print "starting #", n

            factfind(primes(n),n)
            print primfac

            del primfac
            primfac = []
            simplify(primfac, n)

            time_elapsed = time.clock() - time_start
            print "time:", time_elapsed

            n1+=1

primfacfind(6,15)
</code></pre>

python