WeBWorK Problems

Allow fraction, decimal or percent, but show fraction as correct answer

Allow fraction, decimal or percent, but show fraction as correct answer

by Jan Hlavacek -
Number of replies: 1
I am trying to write a series of problems using MathObjects in which I want to allow students to enter an answer as either a fraction, a decimal approximation, or percent, but the correct answer should be displayed as a fraction.

I know how to allow fraction and decimal with correct answer showing as either of them, and how to allow decimal and percent with correct answer showing as either of them, but I cannot figure out how to do all three, with fraction as the correct answer.
In reply to Jan Hlavacek

Re: Allow fraction, decimal or percent, but show fraction as correct answer

by Alex Jordan -
As you may be doing to get percents, try using contextPercent.pl (https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/macros/PCC/contextPercent.pl). Make $ans a percent object, but set the flags so that decimals and fractions are acceptable answers too. I think that's the default. That is, I think if $ans=Percent("50%"), a student can type 50%, 0.5, or 1/2 and be marked correct.

Then, to manipulate how to display the correct answer use the correct_ans_latex_string option when calling the answer checker. For example:
ANS($ans->cmp(correct_ans_latex_string=>'\frac{1}{2}'));

If you have $fractionanswer dynamically created as a MathObject Fraction, then I think this would work:
ANS($ans->cmp(correct_ans_latex_string=>($fractionanswer->TeX)));

So in summary, and completely untested:

loadMacros('contextFraction.pl', 'contextPercent.pl');
...
$realanswer = 0.5;
Context("Fraction");
$fractionanswer = Fraction($realanswer);
Context("Percent");
$ans = Percent($realanswer);
...
ANS($ans->cmp(correct_ans_latex_string=>($fractionanswer->TeX)));