## WeBWorK Problems

### Author newbie: answer to be variable - 1

by Murray Eisenberg -
Number of replies: 2
Your answer to the following question will help this utter newbie WeBWorK author get started...

I've created a variable:

$num = random(2,6,1); I want to use variable$new for the correct answer, and its value should be $num-1. I know to put ANS($new->cmp() );

after the question statement. But what do I put up in the initialization to compute $new? I tried, right after the definition of$num, using:

$new =$num - 1;

but I get a to-me mysterious tranlator error that I cannot all method "cmp" without a package or object reference.

P.S. In this message, I wanted to make the code to appear in a proper font for code. Can I just use typical <code> </code> tags in this editor? Note that I don't see any option here for previewing my post (unlike in most forums I read).

When you wrote $new->cmp, you are asking WW to use the "cmp" method for the object "$new". The method you used to create $new was as a simple variable assignment, a scalar. Scalars do not know how to do comparisons for answer checking. The MathObjects.pl library allows you to create objects that are aware of such methods. ( see http://webwork.maa.org/wiki/Introduction_to_MathObjects ) If you were to change your line$new = $num-1; to the MathObjects-aware line$new = Compute("$num-1"); then the comparison will be correctly performed. Instead of being a scalar,$new is now an object of type Real that knows how to interpret the cmp method. In addition, the correct answer will show how to compute the answer ($num-1) instead of just the value. If you really don't want to use MathObjects library, then there is an answer checker for comparing numbers. You could have gotten rid of$new altogether and used