## WeBWorK Problems

### The Correct answer displayed changes after user input ### The Correct answer displayed changes after user input

by Bruce Yoshiwara -
Number of replies: 3
When I preview this exercise, I see the correct answer looking as expected if I do not enter any answer. When I do enter an answer and check, both the entered answer and the correct answer have the first two characters transposed, e.g. “89/” instead of “8/9”

DOCUMENT();

"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGchoicemacros.pl",
);

TEXT(beginproblem());

##############################################################
#
Context("Numeric");
Context()->flags->set( reduceConstants => 0);

$d = random(3,9);$n = $d - 1;$d1 = random(3,9);
$n1 =$d1 - 1;
do {$d2 = random(2,9)} until ( gcd($d2 , $d1) == 1);$n2 = $d2 + 1;$a = Formula(" $n1 /$d1");

$f = " \dfrac{$n1}{$d1}\left(\dfrac{$n2}{$d2} \right)^x "; ############################################################## BEGIN_PGML Find the [y]-intercept of each exponential function and decide whether the graph is increasing or decreasing. [ h(x)= [$f ] ]: The [y]-intercept is [___](Enter only the [y]-coordinate.)

END_PGML
Context()->normalStrings;
ANS( checkbox_cmp( $a->correct_ans() ) ); ENDDOCUMENT(); Could someone please explain what’s going on? Yeah, the anomaly disappears when I simply include the answer within the PGML. The reason I don’t include the answer within the PGML is that this is just a snippet of a problem of eight parts that alternate between fill-ins (as above) and multiple-choice. I have not yet learned how to include the answer to a MC within PGML and have instead put the answers afterwards. The anomaly persists if I give up on PGML but include all eight parts. As far as I can tell, the answer checkers are marking answers correctly, it's just that the displayed Answer Preview and Correct Answer are off. Thanks. Bruce Yoshiwara In reply to Bruce Yoshiwara ### Re: The Correct answer displayed changes after user input by Alex Jordan - Hi Bruce, The answer check line you have is: ANS( checkbox_cmp($a->correct_ans() ) );

That looks like it is something you are supposed to use with some type of multiple choice question and object. Instead, $a is a MathObject. So what you need is: ANS($a->cmp() ) );

Also, feel free to send me a "final" version of the full problem, and I should be able to offer you back a streamlined version all in PGML that might help things go more quickly with your problem development. ### Re: The Correct answer displayed changes after user input

by Bruce Yoshiwara -
Thanks Alex! That change makes all appear normal.

Cheers.

Bruce ### Re: The Correct answer displayed changes after user input

by Danny Glin -
One more little thing about the code you posted:

The line
$a = Formula("$n1 / $d1"); casts$a as a formula, which is treated as a function of whatever variables are allowed in the current context, which by default is just x. That means that WeBWorK won't complain if students enter a function of x as their answer.

You should probably have the following instead:
$a = Compute("$n1 / \$d1" );

This should recognize the correct answer as being a real number, which means that students will get a warning message if they enter a function of x, telling them that it is expecting a number.

Using "Compute" rather than "Real" has the advantage of preserving the formatting of the answer as you entered it, i.e. leaving it as a fraction rather than storing the decimal version.