WeBWorK Problems

MultiAnswer with named answer rules

MultiAnswer with named answer rules

by Alex Jordan -
Number of replies: 11
I have not figured out how to use parserMultiAnswer.pl when the answer blanks which are tied together have some other answer in between. I gather that named answer rules need to be used, and that the namedRules option needs to be set to true.

I also searched the OPL and there are no instances of 'namedRules' so there isn't an example there I could mimic.

Is anyone able to tweak this minimal example so that it works? Right now it makes warning messages about no answer evaluator for the answer blanks.


DOCUMENT();

loadMacros("PGstandard.pl","MathObjects.pl","parserMultiAnswer.pl",);

TEXT(beginproblem());

Context('Numeric');
$a = Real(1);
$b = Real(2);
$c = Real(3);

$multians = MultiAnswer($a, $c)->with(
namedRules => 1,
checker => sub {
my ( $correct, $student, $self ) = @_;
my ( $astu, $cstu ) = @{$student};
if ( $astu + 2 == $cstu ) {
return [1,1];
} else {return [0,0];}
}
);

BEGIN_TEXT
Enter some number for \(a\): \{$multians->ans_rule(6)\}
$PAR
The number \(b\) is \(2\); enter \(2\) here: \{NAMED_ANS_RULE('b',6)\}
$PAR
The number \(c\) is \(a+b\); enter \(c\) here: \{$multians->ans_rule(6)\}
END_TEXT

NAMED_ANS($multians->cmp);
NAMED_ANS('b', $b->cmp);

ENDDOCUMENT();
In reply to Alex Jordan

Re: MultiAnswer with named answer rules

by Glenn Rice -
Something is broken with the namedRules option in the MultiAnswer macro. There is a work around that I found that works in the case that the singleResult option is also used. Here is a modification of your problem that demonstrates the workaround.

DOCUMENT();

loadMacros("PGstandard.pl","MathObjects.pl","parserMultiAnswer.pl",);

TEXT(beginproblem());

Context('Numeric');
$a = Real(1);
$b = Real(2);
$c = Real(3);

$multians = MultiAnswer($a, $c)->with(
namedRules => 1,
singleResult => 1,
checker => sub {
my ( $correct, $student, $self ) = @_;
my ( $astu, $cstu ) = @{$student};
if ( $astu + 2 == $cstu ) {
return [1,1];
} else {return [0,0];}
}
);
@multians_cmp = $multians->cmp;
$multians->{part}++;

BEGIN_TEXT
Enter some number for \(a\): \{NAMED_ANS_RULE($multians_cmp[0], 6)\}
$PAR
The number \(b\) is \(2\); enter \(2\) here: \{NAMED_ANS_RULE('b',6)\}
$PAR
The number \(c\) is \(a+b\); enter \(c\) here: \{$multians->ans_rule(6)\}
END_TEXT

NAMED_ANS(@multians_cmp);
NAMED_ANS('b', $b->cmp);

ENDDOCUMENT();
In reply to Glenn Rice

Re: MultiAnswer with named answer rules

by Glenn Rice -
By the way, even if you delete all of the stuff with b from the problem, but leave the multi answer part of the problem alone it gives the same errors.
In reply to Alex Jordan

Re: MultiAnswer with named answer rules

by Glenn Rice -
I guess it should have been obvious from my previous example, but there is a similar workaround that works without the single answer option.

DOCUMENT();

loadMacros("PGstandard.pl","MathObjects.pl","parserMultiAnswer.pl",);

TEXT(beginproblem());

Context('Numeric');
$a = Real(1);
$b = Real(2);
$c = Real(3);

$multians = MultiAnswer($a, $c)->with(
namedRules => 1,
checker => sub {
my ( $correct, $student, $self ) = @_;
my ( $astu, $cstu ) = @{$student};
if ( $astu + 2 == $cstu ) {
return [1, 1];
} else {return [0, 0];}
}
);
@multians_cmp = $multians->cmp;
$multians->{part}++;
$multians->{part}++;

BEGIN_TEXT
Enter some number for \(a\): \{NAMED_ANS_RULE($multians_cmp[0], 6)\}
$PAR
The number \(b\) is \(2\); enter \(2\) here: \{NAMED_ANS_RULE('b',6)\}
$PAR
The number \(c\) is \(a+b\); enter \(c\) here: \{NAMED_ANS_RULE($multians_cmp[2], 6)\}
END_TEXT

NAMED_ANS(@multians_cmp);
NAMED_ANS('b', $b->cmp);

ENDDOCUMENT();
In reply to Alex Jordan

Re: MultiAnswer with named answer rules

by Davide Cervone -
An alternative would be to not use namedRules for the MultiAnswer object, but do use a named answer rule for the intermediate answer. That will make the two multi-answer blanks be consecutive non-named blanks, and the MultiAnswer object will attach to the properly without names.

Here is an example:

DOCUMENT();

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "parserMultiAnswer.pl"
);

TEXT(beginproblem());

Context('Numeric');
$a = Real(1);
$b = Real(2);
$c = Real(3);

$multians = MultiAnswer($a, $c)->with(
  checker => sub {
    my ( $correct, $student, $self ) = @_;
    my ( $astu, $cstu ) = @{$student};
    if ( $astu + 2 == $cstu ) {
      return [1,1];
    } else {
      return [0,0];
    }
  }
);

BEGIN_TEXT
Enter some number for \(a\): \{$multians->ans_rule(6)\}
$PAR
The number \(b\) is \(2\); enter \(2\) here: \{NAMED_ANS_RULE('b',6)\}
$PAR
The number \(c\) is \(a+b\); enter \(c\) here: \{$multians->ans_rule(6)\}
END_TEXT

ANS($multians->cmp);
NAMED_ANS(b => $b->cmp);

ENDDOCUMENT();
In reply to Davide Cervone

Re: MultiAnswer with named answer rules

by Glenn Rice -
I have a pending pull request that makes Alex's original example work and fixes the underlying issue.

While your workaround will work (as does mine), I don't think that your workaround will work if, for example, there were two multi-answers with intermingled answer rules. My work-around might work there, but it seems to me that the proper way to proceed is to fix the underlying issue. Michael Gage has requested a review of my pull request from both of you. Could you take a look at it?
In reply to Glenn Rice

Re: MultiAnswer with named answer rules

by Alex Jordan -
Hi Glenn, and thank you for the fix. It's on my list to review it. It lost some urgency on my end when I ended up just putting all the answers into the multianswer, even the one that doesn't really interact with the others. But I still intend to review it as soon as some local demands settle down.
In reply to Alex Jordan

Re: MultiAnswer with named answer rules

by Glenn Rice -
No problem. This is my first pull request to WeBWorK, so I am excited to get one in and become a contributor.
In reply to Glenn Rice

Re: MultiAnswer with named answer rules

by Davide Cervone -
I don't think that your workaround will work if, for example, there were two multi-answers with intermingled answer rules

You are correct, it would not. It wasn't intended as a general fix, only as an alternative that doesn't require hacking the MultiAnswer output.
In reply to Davide Cervone

Re: MultiAnswer with named answer rules

by Glenn Rice -
Yeah, my approach does hack the MultiAnswer. Thanks for looking at my pull request. I made your suggested changes.