Ask Your Question

local variable 'Integer' referenced before assignment

asked 2018-03-14 02:12:23 +0200

anonymous user


I'll be honest I have no idea why this error is popping up, and it's really weird! I'm writing code that will generate a closed form solution to the partial sums of integer powers up to n, and this is what I have

def sum_first_n_p_powers(p):
    length = p+1
    vector = zero_vector(length)
    matrix_list = []
    for x in xrange(0,length):
        copy = vector[:]
        for y in xrange(0,length): 
            copy[y] = binomial(length-y,x-y+1)
    M = Matrix(matrix_list)
    solution_vector = zero_vector(length)
    solution_vector[0] = 1
    coeffs = M.solve_right(solution_vector)
    n = var('n')
    0 = polynomial
    for x in xrange(0,len(coeffs)):
        polynomial = polynomial + coeffs[x]*n^(length-x)
    return polynomial

And when I try to run it I get the most unusual error, it just says to me

UnboundLocalError: local variable 'Integer' referenced before assignment. I have never run into this before and have no idea what's wrong with the line. Thanks in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-03-14 04:08:46 +0200

kcrisman gravatar image

Try this change:

    polynomial = 0

Seems to do what you want then; naturally 0 = polynomial is not valid Python in any case.

I don't know how that even got through the preparser without a worse error indicating what you did wrong more obviously - I have submitted Trac 24971 regarding this very subtle thing, which seems to be a double preparsing of Sage stuff in this case.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-03-14 02:12:23 +0200

Seen: 1,427 times

Last updated: Mar 14 '18