Ask Your Question

Strange type error in interactive

asked 2013-10-26 17:18:17 -0600

jaia gravatar image

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

#Problems correct (in a row and total)
streak = 0
tot = 0
first = True

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):
         first = False

    #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)
            html("Try again")
            streak = 0
        print "Enter your answer"
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-10-26 22:42:58 -0600

ndomes gravatar image



I had to set (don't know why)

global rand_poly

to get it work

A further suggestion (looks better):

#instead of:  html("f(x)=")+html(rand_poly)
edit flag offensive delete link more


Thanks! 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.)

jaia gravatar imagejaia ( 2013-10-27 10:48:03 -0600 )edit

Even better: [Sage tables]( are now a top-level thing.

kcrisman gravatar imagekcrisman ( 2013-10-27 14:22:09 -0600 )edit

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: 2013-10-26 17:18:17 -0600

Seen: 67 times

Last updated: Oct 26 '13