WeBWorK Problems

Student defined variables

Student defined variables

by Zak Zarychta -
Number of replies: 2

Can I use student input as data for further calculations? If so How might I do this?


The context for doing this is that I wish to construct a guided laboratory proforma where student input of independent and dependent variables from an experiment are used to calculate other quantities which are compared with student calculated values input into a certain part of a table.


It would be great to hear from someone who has attempted this sort of thing already.


Thanks in advance for any guidance on this.

Regards,

Zak


In reply to Zak Zarychta

Re: Student defined variables

by Michael Gage -
This can be done using $inputs_ref which is a hash to the answers submitted by students.

It's still largely a do-it-yourself construction. If we figure out use case which is sufficiently general we may be able to simplify it.

It helps if you can see the contents of the hashes involved:

Advanced Problem Debugging: Users with admin privileges will have the ability to see detailed debugging information about problems. There are four options available. AnswerGroupInfo shows information about answer "groups". This is most helpful for multianswer style problems. Show Auxiliary Resources shows information about auxiliary files used by the problem. AnswerHashInfo shows information about the answer hash, including answers, filters and evaluators. PGInfo prints the PG environment, including all available variables.


Search for "inputs_ref" here: http://webwork.maa.org/moodle/mod/forum/discuss.php?d=1685
to see some of the low level functions. You should also look at MultiAnswer (which replaces MultiPart) to see if some of the higher level answer checkers meet your needs.


In reply to Zak Zarychta

Re: Student defined variables

by Davide Cervone -
If you don't need to actually display the student's value later in the problem, you might be able to use the MultiAnswer object to tie several answers together.

For example, here is a problem that asks the students to come up with a value for a that makes x3 - ax not be one-to-one, and then asks for two values of x where the values are the same.


loadMacros("parserMultiAnswer.pl", "PGML.pl");

Context("Numeric");

$ma = MultiAnswer(-1, 0, 1)->with(
  checker => sub {
    my ($c, $s, $ans) = @_;
    my ($a, $x1, $x2) = @$s;
    my $aOK = ($a->value < 0);
    my $f = Compute("x^3 + ($a) x");
    my $xOK = $x1 != $x2 && $f->eval(x => $x1) == $f->eval(x => $x2);
    return [$aOK, $xOK, $xOK];
  }
);

BEGIN_PGML
Consider the function [`f(x) = x^3 + a x`].

Find a value of [`a`] so that [`f`] is *not* one-to-one: 

    [`a`] = [________]{$ma}

Show that, for your value of [`a`], [`f`] is not one-to-one by finding two values, [`x_1`] and [`x_2`] where [`f(x_1) = f(x_2)`]:

    [`x_1`] = [________]{$ma}  
    [`x_2`] = [________]{$ma}

END_PGML

Of course, this one doesn't give very good error messages, and it will mark all three answers incorrect if any one of them is blank, so you might want to get more sophisticated about it, as in the following:
loadMacros("parserMultiAnswer.pl", "PGML.pl");

Context("Numeric");

$ma = MultiAnswer(-1, 0, 1)->with(
  allowBlankAnswers => 1,
  checker => sub {
    my ($c, $s, $ans) = @_;
    my ($a, $x1, $x2) = @$s;
    Value->Error("Your answer can't be checked without a value here") if $a eq '';
    my $aOK = ($a->value < 0);
    $ma->setMessage(2, "Both x1 and x2 must be entered in order to check your answer") if $x1 eq '' && $x2 ne '';
    $ma->setMessage(3, "Both x1 and x2 must be entered in order to check your answer") if $x2 eq '' && $x1 ne '';
    return [$aOK, 0, 0] if ($x1 eq '' || $x2 eq '');
    $ma->setMessage(3, "Your answers for x1 and x2 must be different") if $x1 == $x2;
    my $f = Compute("x^3 + ($a) x");
    my $xOK = $x1 != $x2 && $f->eval(x => $x1) == $f->eval(x => $x2);
    return [$aOK, $xOK, $xOK];
  }
);

BEGIN_PGML
Consider the function [`f(x) = x^3 + a x`].

Find a value of [`a`] so that [`f`] is *not* one-to-one: 

    [`a`] = [________]{$ma}

Show that, for your value of [`a`], [`f`] is not one-to-one by finding two values, [`x_1`] and [`x_2`] where [`f(x_1) = f(x_2)`]:

    [`x_1`] = [________]{$ma}  
    [`x_2`] = [________]{$ma}

END_PGML

Perhaps this approach is good enough for your situation? Otherwise, Mike's suggestion of using $inputs_ref is the way to go. You might find the scaffolding macros to be useful for breaking the problem into sections.