Just passing this thread on to sage-support, since it more properly
belongs there and some people there but not here might have
comments.

Also, is there any conceivable way that this sort of overloading could
somehow automatically make it into the definition of how interact
works, maybe via a keyword "function" or "create_function" or
something like that?  This is probably hopelessly naive, especially
since "function" is probably what one wants to call one of the
variables going in a text box!  But oh well.

- kcrisman

> >> On Thu, Jan 22, 2009 at 9:39 AM, Jonathan Gutow <gu...@uwosh.edu> wrote:
>
> >> > I'm probably being dense here....But would appreciate any suggestions/
> >> > help.  What I want to do is ask students to interactively input a
> >> > variable number of expressions based on some randomly generated
> >> > 'story' problems (actually chemical kinetic mechanisms).  I can
> >> > generate the problems and check them, but cannot figure out how to
> >> > make the @interact construct accept a variable number of
> >> > input_boxes.  Is it possible, and, if so, how?
>
> >> > thanks,
> >> > Jonathan

Luiz said:

This worked for me, but it is definitely hackish and cumbersome. You
basically define the function in a string, exec the string to create
the function, and then use the function interact, instead using the
decoration @interact. (I've read it is frowned up, but...)
# first cell
fields = ['a','b','c','d','e']
init_values = [10,15,31,14,-5, 6]
fields_def = ','.join(['%s=%d' % t for t in zip(fields,init_values)])
fdef= 'def _(' + fields_def + '):\n' \
    + '\tprint sum([' + ','.join(fields) + '])\n'
print fdef   # just to check
# second cell
exec(fdef)
interact(_)

> A better solution might be to use a dictionary:
>  dfields = dict(a=10,b=15,c=31,d=14,e=-5)
>
> I think it would even be possible to write a function with a signature like:
>
> create_function(name, body='pass', **kwargs)
>
> That would generate the string that has to be exec'ed, and the field
> definitions would go in the dictionary kwargs.
>
> One thing I thought is that if you have several interactions in the
> same worksheet, you probably cannot use the same function name `_' for
> all of them, since the functions are created in the global namespace.


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to