WeBWorK Problems

How to hint when a limitedNumeric answer is out of range

How to hint when a limitedNumeric answer is out of range

by Nicole Wilson -
Number of replies: 5
I am writing a probability question and I would like to give students the hint
"Probabilities cannot be negative and cannot exceed one." if they enter a negative value or a value greater than 1.

All the AnswerHints examples on the wiki/moodle pages require an individual value or mathObject.

Can I use AnswerHints to do this? If not, what is method is suitable?
In reply to Nicole Wilson

Re: How to hint when a limitedNumeric answer is out of range

by Alex Jordan -
Hi Nicole,

Here is where you can see the answerHints.pl file:
https://github.com/openwebwork/pg/blob/master/macros/answerHints.pl

On line 83 an example starts, and at line 88 you can see a subroutine that is used to trigger a feedback message. Actually another one at line 97 is closer to your use case.

As an aside, I have often thought about having a context for "strict probabilities" where answers must be between 0 and 1 and that would be built into all the comparison operators. It would have other special things like allow the percent sign to be used, and have a special tolerance mechanism based on proximity to either 0 or 1 (the default relative tolerance is based on proximity to 0 only). But so far it is just an idea.
In reply to Alex Jordan

Re: How to hint when a limitedNumeric answer is out of range

by Nicole Wilson -
Thanks for getting back to me so quickly, Alex.

That's got it.

I like the idea of having a context for "strict probabilities". It would make probability questions much shorter.
My current problem has three probabilities so I end up with this:

ANS(Compute($normProbA[0])->cmp()->withPostFilter(AnswerHints(
sub {
 my ($correct,$student,$ans) = @_;
 return $student < 0;
 } => "Probabilities cannot be negative.",
sub {
 my ($correct,$student,$ans) = @_;
 return $student > 1;
 } => "Probabilities cannot be greater than one.",)));
ANS(Compute($normProbB[0])->cmp()->withPostFilter(AnswerHints(
sub {
 my ($correct,$student,$ans) = @_;
 return $student < 0;
 } => "Probabilities cannot be negative.",
sub {
 my ($correct,$student,$ans) = @_;
 return $student > 1;
 } => "Probabilities cannot be greater than one.",)));
ANS(Compute($normProbC)->cmp()->withPostFilter(AnswerHints(
sub {
 my ($correct,$student,$ans) = @_;
 return $student < 0;
 } => "Probabilities cannot be negative.",
sub {
 my ($correct,$student,$ans) = @_;
 return $student > 1;
 } => "Probabilities cannot be greater than one.",)));
In reply to Nicole Wilson

Re: How to hint when a limitedNumeric answer is out of range

by Davide Cervone -
You could simplify this by making only one answer-hint function for each answer, as in:
ANS(Compute($normProbA[0])->cmp->withPostFilter(AnswerHints(
  sub {
    my ($correct,$student,$ans) = @_;
    return $student  < 0 || $student > 1;
   } => "Probabilities must be between 0 and 1."
)));
ANS(Compute($normProbB[0])->cmp->withPostFilter(AnswerHints(
  sub {
    my ($correct,$student,$ans) = @_;
    return $student < 0 || $student > 1;
   } => "Probabilities must be between 0 and 1."
)));
ANS(Compute($normProbC)->cmp->withPostFilter(AnswerHints(
  sub {
    my ($correct,$student,$ans) = @_;
    return $student < 0 || $student > 1;
   } => "Probabilities must be between 0 and 1."
)));
Even better, you could use a common function for all three:
$rangeHint =   sub {
  my ($correct,$student,$ans) = @_;
  return $student < 0 || $student > 1;
};
ANS(Compute($normProbA[0])->cmp->withPostFilter(AnswerHints(
  $rangeHint => "Probabilities must be between 0 and 1."
));
ANS(Compute($normProbB[0])->cmp->withPostFilter(AnswerHints(
  $rangeHint => "Probabilities must be between 0 and 1."
));
ANS(Compute($normProbC)->cmp->withPostFilter(AnswerHints(
  $rangeHint => "Probabilities must be between 0 and 1."
));
Or you could make the whole answer hint into a function:
sub RangeHint {
  AnswerHints(
    sub {
      my ($correct,$student,$ans) = @_;
      return $student < 0 || $student > 1;
    } => "Probabilities must be between 0 and 1."
  );
};
ANS(Compute($normProbA[0])->cmp->withPostFilter(RangeHint));
ANS(Compute($normProbB[0])->cmp->withPostFilter(RangeHint));
ANS(Compute($normProbC)->cmp->withPostFilter(RangeHint));
You could even put the RangeHint function into a macro file and use includeMacros() to load it into you problem so that you can use the same code in multiple problems.
In reply to Davide Cervone

Re: How to hint when a limitedNumeric answer is out of range

by Alex Jordan -
The forum ate part of Davide's post that had less-than and greater-than signs. I think that his post had lines like:

return $student less-than 0 or $student greater-than 1;

with less-than and greater-than being the actual characters for those.
In reply to Alex Jordan

Re: How to hint when a limitedNumeric answer is out of range

by Davide Cervone -
Thanks, and sorry for the error! I have fixed the original post.