On Thu, Jan 22, 2009 at 11:47 AM, Jonathan <gu...@uwosh.edu> wrote: > > Thanks, this looks like it might do what I want. I have some > questions for below on exactly what you are doing. > > On Jan 22, 10:30 am, Luiz Felipe Martins > <luizfelipe.mart...@gmail.com> wrote: >> 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)]) > What is the zip(...) doing? Does this change the type of the values > or is it a sneaky way of > indexing both values at once?
The zip simply makes a list of pairs of the field names and initial values: [('a',10), ('b',15),('c',31),...] and then the pairs are passed to the formatting string. 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. >> fdef= 'def _(' + fields_def + '):\n' \ >> + '\tprint sum([' + ','.join(fields) + '])\n' >> print fdef # just to check >> >> # second cell >> exec(fdef) >> interact(_) >> >> >> >> 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 >> >> > Dr. Jonathan H. Gutow >> > Chemistry Department gu...@uwosh.edu >> > UW-Oshkosh Office:920-424-1326 >> > 800 Algoma Boulevard FAX:920-424-2042 >> > Oshkosh, WI 54901 >> > http://www.uwosh.edu/faculty_staff/gutow/ >> >> -- >> "The main things which seem to me important on their own account, and >> not merely as means to other things, are knowledge, art, instinctive >> happiness, and relations of friendship or affection." >> -Bertrand Russell >> >> L. Felipe Martins >> Department of Mathematics >> Cleveland State University >> luizfelipe.mart...@gmail.com > > > -- "The main things which seem to me important on their own account, and not merely as means to other things, are knowledge, art, instinctive happiness, and relations of friendship or affection." -Bertrand Russell L. Felipe Martins Department of Mathematics Cleveland State University luizfelipe.mart...@gmail.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sage-edu" group. To post to this group, send email to sage-edu@googlegroups.com To unsubscribe from this group, send email to sage-edu+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-edu?hl=en -~----------~----~----~----~------~----~------~--~---