ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 27 Oct 2013 20:22:09 +0100Strange type error in interactivehttps://ask.sagemath.org/question/10662/strange-type-error-in-interactive/I'm trying to make a simple interactive for my students to practice differentiating polynomials. For some reason, when an answer is entered, Sage tries to convert it (a polynomial) to an instance of sage.structure.parent.Parent and chokes. The same code run outside of an interactive works fine. Why is Sage trying to do this conversion and how do I get it to stop?
#Create polynomial generator
poly.<x>=PolynomialRing(QQ)
#Problems correct (in a row and total)
streak = 0
tot = 0
first = True
@interact
def polyDiff(ans=input_box(label="f'(x)=", type=sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint)):
#Load persistent variables
global streak
global tot
global first
global poly
#Set up and display problem on first run
if (first == True):
rand_poly=poly.random_element(2)
first = False
html("f(x)=")+html(rand_poly)
#Define correct answer
corr = diff(rand_poly, x)
#Check student's answer
if (ans is not None):
if (corr == ans):
print "Correct!"
tot += 1
streak += 1
#Generate new problem
rand_poly = poly.random_element(5)
else:
html("Try again")
streak = 0
else:
print "Enter your answer"
Sun, 27 Oct 2013 00:18:17 +0200https://ask.sagemath.org/question/10662/strange-type-error-in-interactive/Answer by ndomes for <p>I'm trying to make a simple interactive for my students to practice differentiating polynomials. For some reason, when an answer is entered, Sage tries to convert it (a polynomial) to an instance of sage.structure.parent.Parent and chokes. The same code run outside of an interactive works fine. Why is Sage trying to do this conversion and how do I get it to stop?</p>
<pre><code>#Create polynomial generator
poly.<x>=PolynomialRing(QQ)
#Problems correct (in a row and total)
streak = 0
tot = 0
first = True
@interact
def polyDiff(ans=input_box(label="f'(x)=", type=sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint)):
#Load persistent variables
global streak
global tot
global first
global poly
#Set up and display problem on first run
if (first == True):
rand_poly=poly.random_element(2)
first = False
html("f(x)=")+html(rand_poly)
#Define correct answer
corr = diff(rand_poly, x)
#Check student's answer
if (ans is not None):
if (corr == ans):
print "Correct!"
tot += 1
streak += 1
#Generate new problem
rand_poly = poly.random_element(5)
else:
html("Try again")
streak = 0
else:
print "Enter your answer"
</code></pre>
https://ask.sagemath.org/question/10662/strange-type-error-in-interactive/?answer=15616#post-id-15616Omit:
type=sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint
I had to set (don't know why)
global rand_poly
to get it work
A further suggestion (looks better):
html.table([("$f(x)=$",rand_poly)])
#instead of: html("f(x)=")+html(rand_poly)Sun, 27 Oct 2013 04:42:58 +0100https://ask.sagemath.org/question/10662/strange-type-error-in-interactive/?answer=15616#post-id-15616Comment by jaia for <p>Omit:</p>
<pre><code>type=sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint
</code></pre>
<p>I had to set (don't know why)</p>
<pre><code>global rand_poly
</code></pre>
<p>to get it work</p>
<p>A further suggestion (looks better):</p>
<pre><code>html.table([("$f(x)=$",rand_poly)])
#instead of: html("f(x)=")+html(rand_poly)
</code></pre>
https://ask.sagemath.org/question/10662/strange-type-error-in-interactive/?comment=16845#post-id-16845Thanks! That works. (Note to anyone wanting to use this code: move the display line to the end of the function to get the interactive to update correctly after the first problem.)Sun, 27 Oct 2013 16:48:03 +0100https://ask.sagemath.org/question/10662/strange-type-error-in-interactive/?comment=16845#post-id-16845Comment by kcrisman for <p>Omit:</p>
<pre><code>type=sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint
</code></pre>
<p>I had to set (don't know why)</p>
<pre><code>global rand_poly
</code></pre>
<p>to get it work</p>
<p>A further suggestion (looks better):</p>
<pre><code>html.table([("$f(x)=$",rand_poly)])
#instead of: html("f(x)=")+html(rand_poly)
</code></pre>
https://ask.sagemath.org/question/10662/strange-type-error-in-interactive/?comment=16843#post-id-16843Even better: [Sage tables](http://sagemath.org/doc/reference/misc/sage/misc/table.html?highlight=table#sage.misc.table.table) are now a top-level thing.Sun, 27 Oct 2013 20:22:09 +0100https://ask.sagemath.org/question/10662/strange-type-error-in-interactive/?comment=16843#post-id-16843