WeBWorK Problems

Author newbie: answer to be variable - 1

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).

In reply to Murray Eisenberg

Re: Author newbie: answer to be variable - 1

by D. Brian Walton -
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

ANS( num_cmp($num-1) );

- Brian Walton
In reply to D. Brian Walton

Re: Author newbie: answer to be variable - 1

by Davide Cervone -
Brian has given you the correct advice, but I thought I'd add two comments.

First, be sure to load the MathObjects.pl macros via

at the top of your problem.

Second, if you don't want the correct answer to appear as $num-1 (e.g., if $num is 3, the correct answer will show as 3-1), then you can do either

    $new = Compute($num-1);
(with no quotation marks, so the subtraction is done first and THEN Compute() is called on the result), or
    $new = Real($num-1);
which creates a Real MathObject directly.

Good luck with your authoring.

Davide PS, if you are using the reply form to enter your message, you can enter <code&gt...&lt/code> directly, but if you are using the WYSIWYG editor, then you can't enter HTML directly (though you could probably select a monospace font). I have also been unhappy about no preview, but once you post you can continue to edit for 30 minutes, so that's almost like a preview.