ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 13 Mar 2018 22:08:46 -0500local variable 'Integer' referenced before assignmenthttps://ask.sagemath.org/question/41524/local-variable-integer-referenced-before-assignment/ 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)
matrix_list.append(copy)
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!
Tue, 13 Mar 2018 20:12:23 -0500https://ask.sagemath.org/question/41524/local-variable-integer-referenced-before-assignment/Answer by kcrisman for <p>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</p>
<pre><code>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)
matrix_list.append(copy)
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
</code></pre>
<p>And when I try to run it I get the most unusual error, it just says to me</p>
<p>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!</p>
https://ask.sagemath.org/question/41524/local-variable-integer-referenced-before-assignment/?answer=41533#post-id-41533Try 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](https://trac.sagemath.org/ticket/24971) regarding this very subtle thing, which seems to be a double preparsing of Sage stuff in this case.Tue, 13 Mar 2018 22:08:46 -0500https://ask.sagemath.org/question/41524/local-variable-integer-referenced-before-assignment/?answer=41533#post-id-41533