WeBWorK Problems

Problem with answerHints

Problem with answerHints

by Robin Cruz -
Number of replies: 2

Hi, all,

I cannot get answerHints to work in a problem I'm trying to write.  (ww_version: 2.19 | pg_version 2.19) I've including a snippet of the problem below.  

1) It does not send the error message when the conditions are met

2) The computed answer will not show up (I'm including a photo of that shows up when it is referenced)

3) A warning message is given when the answer is checked (also in the photo)

Thanks -- rac

--------------Problem snippet-------------------------

DOCUMENT();

l oadMacros(

  "PGstandard.pl",

  "MathObjects.pl",

  "PGML.pl",

  "parserNumberWithUnits.pl",

  "answerHints.pl",

);

Context()->flags->set(

    tolerance => 2,  #--use more tolerance since reading from a graph----

    tolType   => 'absolute',

);

$distance = 9;  #--test----

$km1 = NumberWithUnits("$distance km")->cmp->withPostFilter(AnswerHints(

    sub {

         my ($correct, $student, @ans) = @_;

         return (abs($correct-$student)<5 && abs($correct-$student)>2);

         } => ['Your answer is close. Look at the graph more carefully.']

));

###############################

BEGIN_PGML

What is the distance between the towns (answer: [$distance] km) :  [_]{$km1}

[`[$km1]`]

END_PGML

ENDDOCUMENT();

In reply to Robin Cruz

Re: Problem with answerHints

by Jaimos Skriletz -
Hello. It appears the issue is that the object NumberWithUnits cannot be compared directly to a number like 2 or 5 using less than or greater than. You will need to do the comparison to another number with units. I was able to get your problem to almost work by updating your return statement as follows:

return (abs($correct - $student) < NumberWithUnits('5km') && abs($correct - $student) > NumberWithUnits('2km'));

The issue is that greater than and less than are just not working well with NumberWithUnits (in pg 2.20 there will be a new contextUnits that will work much better in this regard). For some reason '3km' > '2km' returns false, so I was not able to nicely get comparisons to work directly. In trying to track this down I tested lots of things and also found that even subtraction of NumberWithUnits doesn't always work well. For instance I tested that NumberWithUnits('9km') - NumberWithUnits('6m') and got '3km' as the result. So really this old legacy macro will be better replaced with the new context...but here is the best work around I can find. Inside your sub, I would do the following:

my $diff = abs($correct->value - $student->value);
return $correct->{units} eq $student->{units} && $diff > 2 && $diff < 5;

# This gives a number to compare, I then force the units to be the same, as I didn't find a nice way to translate units with this older macro. This could probably be improved, but might be a starting point.
In reply to Jaimos Skriletz

Re: Problem with answerHints

by Robin Cruz -

Thanks, Jaimos!

It will be good to have the new context. For now, your work-a-round works.

--rac