WeBWorK Problems

Displaying non-decimal correct answer

Displaying non-decimal correct answer

by Adam Weyhaupt -
Number of replies: 6
Hi all-

Consider the following basic problem:

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

DOCUMENT();

loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
#"source.pl", # allows code to be displayed on certain sites.
#"PGcourse.pl", # Customization file for the course
"PGasu.pl"
);

# Print problem number and point value (weight) for the problem
TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;


#
# Setup
#
#
Context("Numeric");

$pi = Real("pi");

#
# Text
#
#

Context()->texStrings;
BEGIN_TEXT

Enter an exact value for \(\pi\):
\{ans_rule(30)\}
END_TEXT
Context()->normalStrings;

# Answer
$sols = $pi;
Parser::Number::NoDecimals(Context());
ANS($sols->cmp());

ENDDOCUMENT();

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

When the correct answer is displayed to the student, the correct at answer is displayed to the student as 3.14159 instead of pi. Is there a way to code the problem so that 'pi' is displayed? I have been told previously that this correct answer could not currently be shown in typeset mode. However, here the 3.14159 is pretty misleading, since cut and pasting that into WW will not be marked correct.

Thanks for any guidance you can provide,

Adam
In reply to Adam Weyhaupt

Re: Displaying non-decimal correct answer

by Arnold Pizer -
Hi Adam,

Davide may have a better way, but you can override what WeBWorK displays as the correct answer by using cmp's correct_ans flag. E.g. in your code replace

ANS($sols->cmp());

by

ANS($sols->cmp(correct_ans=>'pi'));

Arnie


In reply to Adam Weyhaupt

Re: Displaying non-decimal correct answer

by Michael Gage -
Hi Adam,

We're beginning to collect a significant number of wiki pages that can help
with authoring problems. Here are some pointers:

http://webwork.maa.org/wiki/Category:Authors -- top level

http://webwork.maa.org/wiki/Category:Problem_Techniques -- started by Gavin LaRose -- and since it's a wiki you can help add to it :-)

http://webwork.maa.org/wiki/Category:MathObjects

and here is a lab that allows you to test code fragments (the first lab is the most useful at the moment -- the PGML stuff is for the future)

http://webwork.maa.org/wiki/PGLabs



a modification of the example on that page indicates another way to handle the issue you raise. "Compute" like Formula converts strings to objects but with less overhead -- and it maintains a string that it uses by default for the correct answer.

Context("LimitedNumeric");
$ans = Compute("pi");
Context()->constants->remove("pi");
TEXT($ans->cmp->evaluate("pi")->pretty_print );


the last line prints out the entire AnswerHash

-- correct_ans is used as the message in responding to students

-- score =1 or 0 is whether or not the answer is correct.


Most importantly this MathObjects calculator allows you to quickly test things out.


The construction

Context("LimitedNumeric");
$ans = Compute("pi");
Context()->constants->remove("pi");
$ans= Compute($ans)->with(format=>'%0.12G');
TEXT($ans->with(correct_ans => $ans)->cmp->evaluate("pi")->pretty_print );


might also be of interest -- although there may be a simpler way to accomplish the same thing


                                    
In reply to Adam Weyhaupt

Re: Displaying non-decimal correct answer

by Davide Cervone -
Both Arnie's and Mike's answers are indicating the solution to your problem, which is to get the correct_ans set. Arnie does this explicitly using the with method, but Mike uses a subtler approach, which is to use Compute(), since this sets the correct_ans value automatically. So I'd suggest $pi = Compute("pi"); as the solution.

Note that $pi = Real("pi"); is redundant, as $pi = pi; is sufficient (since pi is already defined as a function that returns the value of pi from the current context, and that is a real value already).

I'm also not sure why you feel the need to set the value $sol, since ANS($pi->cmp) would work just as well. Indeed, you do not need the $pi variable either, as ANS(pi->cmp) should do it.

Mike may have misunderstood what you are trying to accomplish, since he suggested the use of the LimitedNumeric from which pi has been removed. That would prevent students from entering the answer as "pi" and would force them to use decimals. You seem to want the opposite: they must not use decimals and must enter pi symbolically. For that, you are correct to use the NoDecimals function.

Davide

In reply to Davide Cervone

Re: Displaying non-decimal correct answer

by Adam Weyhaupt -
Thanks for the reply, Davide, Mike, and Arnie. This was a simplification of another problem I was working on, where I had the $sol variable set for readability. I should have taken this out...

Arnie's solution did what I was expecting: on the "answer" page, where several boxes are shown, "Entered" displays 3.14159, "Answer preview" displays a nicely typeset \pi, and "Correct" displays 'pi' (no quotes).

Davide, your solution did not do what I was expecting --- the "Correct" box still displayed 3.14159 (although of course the problem was graded correctly).

Perhaps I misunderstood your reply? I'm using the Rochester "hosted2" Webwork server, at the moment.

Adam
In reply to Adam Weyhaupt

Re: Displaying non-decimal correct answer

by Davide Cervone -
Hmmm. Changing
   $pi = Real("pi");
to
  $pi = Compute("pi");
works for me. What exactly did you do?

The stuff about using pi directly was only to clarify why $pi = Real("pi") was redundant. It doesn't fix your problem with the correct answer. But using Compute() should.

Davide

In reply to Davide Cervone

Re: Displaying non-decimal correct answer

by Adam Weyhaupt -
That's been a few weeks, so I've forgotten exactly what I should have done. However -

1) You are right - changing Real("pi") to Compute("pi") did work.

2) However, ANS(pi->cmp()) did not work (it showed 3.14159 as the correct answer).

Thanks for your help,

Adam