WeBWorK Problems

Help with custom answer checker

Help with custom answer checker

by Isaias Chairez Uriarte -
Number of replies: 7

Hello,

first time poster. I'm a Webwork admin and teacher at a community college. A colleague is trying to use the following code but keeps on getting an error message when the wrong answer is entered. When the correct answer is entered there is no problem.


The error message is the following

Use of uninitialized value in concatenation (.) or string at line 27 of [TMPL]/macros/fixedPrecision.pl

I believe the issue has to do with the custom answer checker. I've been making small tweaks to the answer checker, but don't seem to be able to get rid of the error.

Would appreciate it if someone out there had some insight into this.


DOCUMENT();

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "PGML.pl",
  "PCCmacros.pl",
  "contextFraction.pl",
  "PGcourse.pl",
  "answerHints.pl",
);

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

Context("LimitedNumeric");
loadMacros("fixedPrecision.pl");  

$P = random(100, 950, 50);
$r = random(0.02, 0.1, 0.05);
$m = random(2, 4, 12, 365);
$t = random(1, 30);
$n = $t*$m;

$answer = $P*(1+$r/$m)**$n;

ANS(Compute(FixedPrecision($answer,2))->cmp->withPostFilter(AnswerHints(
  sub {
    my ($correct,$student,$ans) = @_;
    return abs($correct-$student) < 0.5;
   } => "Answers must be rounded correctly and have exactly two digits after the decimal."
)));

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

TEXT(beginproblem());

BEGIN_PGML

Evaluate the expression for [`P = [$P]`], [`r = [$r]`], [`m = [$m]`], and [`n = [$n]`].  Round your answer to the nearest hundredth.

[`P {\left(1 + \frac{r}{m} \right)}^{n}=`]  [_______]{$ans}

END_PGML

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

BEGIN_PGML_SOLUTION
END_PGML_SOLUTION

ENDDOCUMENT();


In reply to Isaias Chairez Uriarte

Re: Help with custom answer checker

by Danny Glin -

The error message references [TMPL]/macros/fixedPrecision.pl, which means that a copy of the macro file has been created within the course.  Can you post a copy of the modified fixedPrecision.pl file from that course?

In reply to Danny Glin

Re: Help with custom answer checker

by Isaias Chairez Uriarte -

Here is a copy of the modified fixedPrecission.pl file from the course.

sub _fixedPrecision_init {}

package FixedPrecision;
our @ISA = ("Value::Real");

sub new {
  my $self = shift; my $class = ref($self) || $self;
  my $context = (Value::isContext($_[0]) ? shift : $self->context);
  my $x = shift; my $n = shift;
  Value::Error("Too many arguments") if scalar(@_) > 0;
  if (defined($n)) {
    $x = main::prfmt($x,"%.${n}f");
  } else {
    $x =~ s/\s+//g;
    my ($int,$dec) = split(/\./,$x);
    $n = length($dec);
  }
  $self = bless $self->SUPER::new($context,$x), $class;
  $self->{decimals} = $n; $self->{isValue} = 1;
  return $self;
}

sub string {
  my $self = shift;
  main::prfmt($self->value,"%.".$self->{decimals}."f");
}

sub compare {
  my ($self,$l,$r) = Value::checkOpOrder(@_);
  $l cmp $r;
}

package FixedPrecisionNumber;
our @ISA = ("Parser::Number");

sub new {
  my $self = shift; my $class = ref($self) || $self;
  my $equation = shift; my $context = $equation->{context};
  $self = bless $self->SUPER::new($equation,@_), $class;
  $self->{value} = FixedPrecision->new($self->{value_string});
  return $self;
}

sub string {(shift)->{value}->string(@_)}
sub TeX {(shift)->{value}->TeX(@_)}

package main;

Context()->{parser}{Number} = "FixedPrecisionNumber";

sub FixedPrecision {FixedPrecision->new(@_)};

1;

In reply to Isaias Chairez Uriarte

Re: Help with custom answer checker

by Glenn Rice -

Change line 16 (the line that reads "$n = length($dec);" in sub new) to "$n = length($dec // '');"

In reply to Glenn Rice

Re: Help with custom answer checker

by Isaias Chairez Uriarte -
In reply to Isaias Chairez Uriarte

Re: Help with custom answer checker

by Glenn Rice -

Cut and paste the code I gave. It works.

It should be 

$n = length($dec // '');

Note those are two single quotes after the two forward slashes. There is a space both before and after the two forward slashes.





In reply to Isaias Chairez Uriarte

Re: Help with custom answer checker

by Paul Vojta -

The line

   $n = length($dec //");

is incorrect.  Instead, it should be:

   $n = length($dec //'');

Note that '' is two single quotes, not one double quote (so two separate characters).

Sincerely,

Paul Vojta

In reply to Danny Glin

Re: Help with custom answer checker

by Andras Balogh -

I am also interested in the solution for this issue.

I get this error message only for incorrect answer, and for me it is loading from [TMPL]/Library/macros/BrockPhysics/fixedPrecision.pl